Welcome Guest, Not a member yet? Register   Sign In
match items in an array against text
#1

[eluser]matt2012[/eluser]
Ok so I have a large array and a large text to search my aim to create a new array based on what items in the large array are found in the text

so something like
Code:
$myarray = array("apple juice","nice cake","ice cream",...)

$my text = "I like apple juice and ice cream"

foreach ($myarray as $item):
  $x = strpos($mytext, $item);
  if (strpos !== 0) $newarray[] = $item;
endforeach;

given that its a large array and large text this seems inefficient is their a better way to tackle this?
#2

[eluser]GSV Sleeper Service[/eluser]
you may be better off using preg_match_all() http://uk3.php.net/manual/en/function.pr...ch-all.php
I expect there's an even neater solution, but I'm not really in the mood for trawling through the php docs!
#3

[eluser]davidbehler[/eluser]
As far as I can tell there is no build-in function that does what you need.
On the strpos - Manual you can find a custom function similar to yours:
Code:
function strposa($haystack ,$needles=array(),$offset=0){
    $chr = array();
    foreach($needles as $needle){
        $chr[] = strpos($haystack,$needle,$offset);
    }
    if(empty($chr)) return false;
    return min($chr);
}




Theme © iAndrew 2016 - Forum software by © MyBB