Please consider this example
Array
(
[1] => search engine optimization
[2] => engine optimization
[0] => search engine
)
I want to remove engine optimization and search engine because these words are already in the longest phrase. These words are not hard coded, the above result is generated by a function. I did my best to code it but couldn’t solve this problem.
EDIT
This is my attempt, looks like people here don’t trust and they are good at pressing vote down button. My approach is not adequate so I didn’t bother to share it here.
$result = array('search engine', 'search engine optimization', 'engine optimization');
uasort($result, 'string_word_length');
foreach($result as $key=>$phrase) {
if( ($found = array_find($phrase, $result, $key)) != FALSE) {
unset($result[$found]);
}
}
function array_find($needle, $haystack, $skip_key = 0) {
if(!is_array($haystack)) return false;
foreach($haystack as $key=>$value) {
if($skip_key != $key && strpos($value, $needle)!==false) {
echo 'Poped ', $value, ' -- ', $key, ' -- ', $skip_key, '<br />';
return $key;
}
}
return false;
}
function string_word_length($a,$b){
return strlen($b) - strlen($a);
}
What about this (supposing you use php >= 5.3):
For those who doesn’t have php 5.3 installed here is the output result:
array(1) { [0]=> string(26) "search engine optimization" }