views:

33

answers:

2

Hello, i have piece of code that works fine on my local test server but on live server for some reason it does not. Php version on live server is 5.1.6.

$subject = 'random words to check';   
$terms = explode(' ', 'word1 word2 check');
$wordIndex = array_flip(preg_split('/\P{L}+/u', mb_strtolower($subject), -1, PREG_SPLIT_NO_EMPTY));
foreach ($terms as $term) {
    if (isset($wordIndex[$term])) {
        echo "match>".$term;
    }
}
A: 

First employ some basic debugging to discover what's happening on each system

  • dump the output of mb_strtolower
  • dump the output of preg_split
  • dump the output of array_flip
Paul Dixon
output of preg_split is empty
Jack
A: 

You might try replacing your preg_split with

$wordIndex = array_flip(str_word_count(mb_strtolower($subject), 2));

Though you may also need the additional 3rd parameter for str_word_count if you're working with multibyte strings

Mark Baker