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.