0

I have a piece of PHP code as below:

$Keywords = array(
            ', JOE.' => '1',
            ', JOE' => '2',
            'JOE' => '3',
            'JOE.' => '4',
            '/JOE' => '5',
            '/JOE/' => '6',
            'JOE/.' => '7',
            ',JOE.' => '8',
            'JOE??>' => '9',
            '; Jo ?' => '10'
    );
$Text = "; Jo ? is ; Jo ? JOE??> is JOE??> is JOE is JOE is JOE is JOE is JOE. Hello , JOE. Hey ,JOE. Come on , JOE. Dude,JOE/. Shut up ,JOE. What is the meaning of /JOE/? Of course, JOE";

extract_keyword ($Keywords, $Text);

function extract_keyword ($Keywords, $Text){
mb_internal_encoding('UTF-8');
print_r($Keywords);
echo "<br>";
uksort($Keywords, function ($a, $b) {
  $as = strlen(preg_replace("/[^A-Za-z0-9 ]/", '', $a));
  $bs = strlen(preg_replace("/[^A-Za-z0-9 ]/", '', $b));

  if ($as > $bs) {
      return -1;
  }
  else if ($bs > $as) {
      return 1;
  }
  return 0;

});

print_r($Keywords);
echo "<br>";

uksort($Keywords, function ($a, $b) {
  $as = mb_strlen($a);
  $bs = mb_strlen($b);

  if ($as > $bs) {
      return -1;
  }
  else if ($bs > $as) {
      return 1;
  }
  return 0;

});

print_r($Keywords);
echo "<br>";   
}

Below you can find the results from the above code:

Array ( [, JOE.] => 1 [, JOE] => 2 [JOE] => 3 [JOE.] => 4 [/JOE] => 5 [/JOE/] => 6 [JOE/.] => 7 [,JOE.] => 8 [JOE??>] => 9 [; Jo ?] => 10 ) 
Array ( [, JOE.] => 1 [; Jo ?] => 10 [, JOE] => 2 [,JOE.] => 8 [JOE??>] => 9 [JOE/.] => 7 [/JOE] => 5 [JOE] => 3 [JOE.] => 4 [/JOE/] => 6 ) 
Array ( [, JOE.] => 1 [JOE??>] => 9 [; Jo ?] => 10 [/JOE/] => 6 [, JOE] => 2 [JOE/.] => 7 [,JOE.] => 8 [JOE.] => 4 [/JOE] => 5 [JOE] => 3 ) 

The curernt code, which is incomplete, prints the $Keywords based on the length of each array elements and length of the non-alphanumeric characters from greatest to least (i.e. Array ( [, JOE.] => 1 [JOE??>] => 9 [; Jo ?] => 10 [/JOE/] => 6 [, JOE] => 2 [JOE/.] => 7 [,JOE.] => 8 [JOE.] => 4 [/JOE] => 5 [JOE] => 3 )). However, I want to print occurance of each keyword (for example [, JOE.] => 1 or [JOE??>] => 9 and etc) that each time appears in the $Text. For better understanding of this purpose, please see the below example.

Please see the below example to understand this question more clearly. Each of the keywords [, JOE.] => 1 or [JOE??>] => 9 or [; Jo ?] => 10 occurred two times in $Text, where [JOE.] => 4 appeared five times and [JOE/.] => 7 appeared once in $Text. So, as a final answer to this question, I want to print all occurrences of each sorted keyword (i.e. each array key with its value Array ( [, JOE.] => 1 [JOE??>] => 9 [; Jo ?] => 10 [/JOE/] => 6 [, JOE] => 2 [JOE/.] => 7 [,JOE.] => 8 [JOE.] => 4 [/JOE] => 5 [JOE] => 3 )) in $Text and complete the below array for all keywords.

Array ( 
[0] => Array ( [, JOE.] => 1 ) 
[1] => Array ( [, JOE.] => 1 ) 
[2] => Array ( [JOE??>] => 9 ) 
[3] => Array ( [JOE??>] => 9 ) 
[4] => Array ( [; Jo ?] => 10 ) 
[5] => Array ( [; Jo ?] => 10 )
...
...
[?] => Array ([JOE/.] => 7)  
...
...
[?] => Array ([JOE.] => 4)
[?] => Array ([JOE.] => 4)
[?] => Array ([JOE.] => 4)
[?] => Array ([JOE.] => 4)
[?] => Array ([JOE.] => 4)
...
)

Could you please see the code and help me to solve this problem? thanks for your time and help.

1 Answer 1

2

I believe this question needs a lot of job so it is totally understoodable. I am not 100% sure I have totally understood the question so the code below may not be what you want but I believe it can be of some help.

$result=array();
// If am not wrong you want for each keyword the count of occurence
foreach( $Keywords as $keyword => $foo){
  $result[strpos($Text, $keyword)][$keyword] = substr_count($Text, $keyword);
}

ksort($result);
$result = array_values($result);

It prints a two dimension array, the integer keys of the outer array declare the order in which a keyowrd was found. The string keys of the inner array contain the defined keywords. The integer values declare the count of occurence for each keyword.

For example:

Array
(
    [0] => Array
        (
            [; Jo ?] => 2
        )

    [1] => Array
        (
            [JOE] => 14
            [JOE??>] => 2
        )

means that "; Jo ?" is the keyword who was found first, it was found in two places.

While "JOE" and "JOE??>" are the keywords who were found second. "JOE" was detected in 14 places while "JOE??>" in 2 palces.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer. what I'm looking is to start checking the Array ( [, JOE.] => 1 [JOE??>] => 9 [; Jo ?] => 10 [/JOE/] => 6 [, JOE] => 2 [JOE/.] => 7 [,JOE.] => 8 [JOE.] => 4 [/JOE] => 5 [JOE] => 3 ) keys and their values from the top (i.e. [, JOE.] => 1, [JOE??>] => 9, [; Jo ?] => 10, and so on) in $Text. Then, if one keyword is matched in $Text then search the rest of the $Text for the next keywords (i.e. last keyword is [JOE] => 3)
@Sami You say "then search the rest of the $Text", does this mean that you don't want to search the text from the begining? You also said "checking the keys and their values from top...", obviously the array keys are considered as keywords, but how about the values, what is their meaning, what exactly you want to check for a value e.g. value 3 for [JOE]=>3, what is that "3", what do you want to do with it? Sorry but it is not very clear to me!
thanks again for your help and time. In fact, I don't want to count the number of occurrence for each keyword. I want to directly print all occurrences foreach keyword with its assigned array key value, something like Array ( [0] => Array ( [, JOE.] => 1 ) [1] => Array ( [, JOE.] => 1 ) [2] => Array ( [JOE??>] => 9 ) [3] => Array ( [JOE??>] => 9 ) [4] => Array ( [; Jo ?] => 10 ) [5] => Array ( [; Jo ?] => 10 ) [6] => Array ([JOE.] => 4) [7] => Array ([JOE.] => 4) [8] => Array ([JOE.] => 4) [9] => Array ([JOE.] => 4) [10] => Array ([JOE.] => 4) [11] => Array ([JOE/.] => 7)
could you please review the question again? I tried to make it more clear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.