0

I have a string:

$string = 'This is Test';

And also an array of words:

$array = array('test','example','blahblah');

I want to look into the $string, and see if there is any of the words of $array in it or not:

$string_arr = explode(' ', $string);
foreach($string_arr as $value){
    if (preg_match("/\b$value\b/iu", $array))
        return true;
}

As you see, I used the 'u' flag for UTF-8 support, but the wired thing is that this works on my wamp(localhost), but on my real server on CentOs it's not working, I googled and I found this: http://chrisjean.com/2009/01/31/unicode-support-on-centos-52-with-php-and-pcre/

But I don not have access to server to upgrade a RPM, so how I should do it?

Thanks in advance


Anybody could come up with another solution? I appreciate any help.

behz4d
  • 1,819
  • 5
  • 35
  • 59
  • You don't have unicode characters in your `$array` of words. Also, you can do this sort of matching with a single regex. Also, you're passing an array to the second param of `preg_match()`, when it expects a string. – nickb May 02 '13 at 13:27

3 Answers3

1

I'm not sure if you can get much simpler than this: array_intersect($array,explode(' ',$string)); You would basically just check if the returned array has any value and that would tell you if any of the words in $array are in $string. The following is tested and works.

if( count(array_intersect($array,explode(' ',$string))) > 0 )
{
    echo 'We have a match!';
}

For the sake of having a full code block...

$string = 'This is Test';
$array = array('test','example','blahblah');
$checked_array = array_intersect($array,explode(' ',$string));

if( count($checked_array) > 0)
{
    echo 'The following words matched: '.implode(', ',$checked_array);
}
Chad
  • 714
  • 2
  • 9
  • 26
0
$testArray = explode(' ', $string);
foreach($testArray as $value){
    if(array_search($value, $testArray) !== false) return true;
}
Nicholas Smith
  • 456
  • 1
  • 6
  • 16
  • This kind of seems like overkill, or at the very least, inefficient. There's no need to loop through all the values individually when you can compare the arrays directly. I disagree with this being the accepted answer. – Chad May 02 '13 at 13:50
  • 1
    @behz4d No problem. I just wanted to make sure you had a substantial answer to your question. It may go without saying but the `$checked_array` variable contains all of the matched words should you need access to those for whatever reason. If you do not, I'd suggest using the first bit of code I provided so you're not wasting memory assigning a variable. – Chad May 02 '13 at 14:02
0

instead of using preg_match use array_search

$key = array_search($string, $array);
if(empty($key))
return true;
else
return false;
  • `in_array()` can be [v](http://stackoverflow.com/questions/6093890/php-in-array-horrible-performance-fatest-way-to-search-array-for-value)[er](http://stackoverflow.com/questions/13483219/what-is-faster-in-array-or-isset)[y](http://forums.phpfreaks.com/topic/237190-in-array-very-slow-performance/) [sl](http://brian.moonspot.net/2008/06/05/in_array-is-quite-slow/)[ow](http://lucb1e.com/?p=post&id=97) – Amal Murali Apr 30 '14 at 07:30