How to use multiple 'needles' in haystack?

How could i select from multiples needles in a haystack using the below code:???



$multipleneedles=("one","two","three");
$wordstofind=(stripos($haystack,$multipleneedles));


if the first needle returns false, go to second needle, if second needle returns false, go to third needle, etc. If no needles are found, ‘default’;

I would really appreciate if someone could help.

You would probably have to write your own function for this, which would involve looping over the array and looking for each one in turn I would imagine. Shouldn’t be too hard.

could you give an example. I rarely ever use arrays, and have a little trouble understanding how they work exactly.

An array acts just like a list of items really, at least in this case that is all it’s doing.

Your function should loop around each element of the array (lookup ‘foreach’ in the php manual), perform a normal stripos() on each element and return the position if there is one (which will break it out the loop & function).

Read up on functions in the php manual, and have a go at coding one yourself, post it here when you are done and I will take a look at it :slight_smile:

will do. if may take a little while. thanks!


<?php

$needles = array( 'mew', 'arf', 'moo' );
$cat = 'The cat goes mew, but will not arf or moo';

var_dump( preg_match( '/\\b(' . join( $needles, '|' ) . ')\\b/i', $cat, $m, PREG_OFFSET_CAPTURE ), $m );
var_dump( preg_match_all( '/\\b(' . join( $needles, '|' ) . ')\\b/i', $cat, $m, PREG_OFFSET_CAPTURE ), $m );


[B]array[/B]
  0 [COLOR=#888a85]=>[/COLOR] 
    [B]array[/B]
      0 [COLOR=#888a85]=>[/COLOR] 
        [B]array[/B]
          0 [COLOR=#888a85]=>[/COLOR] string [COLOR=#cc0000]'mew'[/COLOR] [I](length=3)[/I]
          1 [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]13[/COLOR]
      1 [COLOR=#888a85]=>[/COLOR] 
        [B]array[/B]
          0 [COLOR=#888a85]=>[/COLOR] string [COLOR=#cc0000]'arf'[/COLOR] [I](length=3)[/I]
          1 [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]31[/COLOR]
      2 [COLOR=#888a85]=>[/COLOR] 
        [B]array[/B]
          0 [COLOR=#888a85]=>[/COLOR] string [COLOR=#cc0000]'moo'[/COLOR] [I](length=3)[/I]
          1 [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]38[/COLOR]
  1 [COLOR=#888a85]=>[/COLOR] 
    [B]array[/B]
      0 [COLOR=#888a85]=>[/COLOR] 
        [B]array[/B]
          0 [COLOR=#888a85]=>[/COLOR] string [COLOR=#cc0000]'mew'[/COLOR] [I](length=3)[/I]
          1 [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]13[/COLOR]
      1 [COLOR=#888a85]=>[/COLOR] 
        [B]array[/B]
          0 [COLOR=#888a85]=>[/COLOR] string [COLOR=#cc0000]'arf'[/COLOR] [I](length=3)[/I]
          1 [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]31[/COLOR]
      2 [COLOR=#888a85]=>[/COLOR] 
        [B]array[/B]
          0 [COLOR=#888a85]=>[/COLOR] string [COLOR=#cc0000]'moo'[/COLOR] [I](length=3)[/I]
          1 [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]38[/COLOR]

OK. this is what i got so far, and it’s not working…
so confusing. but it’s all part of the fun.



<?php
$sentence="There is only 1 two in this sentence";
$numbers=array(0=>'one','two','three');
foreach($numbers as $key => $value){
$position=stripos($sentence,$value);
$wordstofind=substr($sentence,$position,3);
echo $wordstofind;
}

?>


Something like this? :slight_smile:


$sub  = 'There is only 1 two in this sentence';
$nums = array( 'one', 'two', 'three' );

foreach ( $nums as $num ) {
    if ( ( $pos = stripos( $sub, $num ) ) !== false ) {
        echo substr( $sub, $pos, strlen( $num ) );
    }
}

i guess. arrays are new to me. what i’m doing is searching through a paragraph for a ‘$needle’ word. if it isn’t present, go to the next one until it finds a match, then echo the sentence that begins with that particular needle. does this make sense? thanks for the help!

I would use a function, and do it roughly like this:

function stripos_multi ($haystack, $needle, $offset) {
 if (!is_array($needle)) {
  $needle = array($needle);
 }//if

 foreach ($needle as $searchstring) {
  $position = stripos($haystack, $searchstring, $offset);

  if ($position !== false) {
   return $position;
  }//if
 }//foreach

 return false;
}//function

thanks, i’ll give it a whirl.

Hey everybody: I worked on it, and this works. Thanks for the help. Here goes:



<?php
$sentence="There is only 1 two in this sentence";
$numbers=array(1=>'one','two');
foreach($numbers as $key => $value){
if(strstr($sentence,$value)){
echo"value of key: $key is <i>$value</i>";
$pos=stripos($sentence,$value);
echo "<br>at position: $pos";
echo "<br>remaining sentence: <i>".substr($sentence,$pos,10)."</i>";
}
}?>


the output is…

value of key: 2 is two
at position: 16
remaining sentence: two in thi

good nite. sleep time. 3rd shift comes early…

By the way, you’ll find your programming much better and easier to read/write if you tab-indent the code, and use a syntax highlighter like Notepad++ or PHPDesigner2008. PHPDesigner2008 is better but but isn’t free.

For example:

<?php
$sentence="There is only 1 two in this sentence";
$numbers=array(1=>'one','two');
foreach($numbers as $key => $value){
    if(strstr($sentence,$value)){
        echo"value of key: $key is <i>$value</i>";
        $pos=stripos($sentence,$value);
        echo "<br>at position: $pos";
        echo "<br>remaining sentence: <i>".substr($sentence,$pos)."</i>";
    }
}
?>

By the way (and the Manual says this), strstr isnt recommended for finding if a string is inside another. Use strpos instead:

<?php
$sentence="There is only 1 two in this sentence";
$numbers=array(1=>'one','two');
foreach($numbers as $key => $value){
    if(($pos = strpos($sentence,$value)) !== false){
        $remaining = substr($sentence,$pos);
        echo <<<RESULT
            <p>
                Value of key: {$key} is <i>{$value}</i><br />
                At position: {$pos}<br />
                remaining sentence: <i>{$remaining}</i><br />
            </p>
            <hr />
RESULT;
    }
}
?>

thanks again. I bet I could learn a lot from you. I do need to write neater code. and i didn’t know that about strstr. maybe that’s what’s slowing it down.

http://uk.php.net/strstr/

If you use Opera, you can set up a simple way of referring to the manual by creating a custom search. All I need to do is go ‘php strstr’ in the address bar and the ‘strstr()’ page in the manual loads.

When using the badly organised array functions (some have the haystack as the first param, some have the needle as the first param) it’s a big time saver :wink:

cool. gotta go though. i’ll be on tomorrow morning. sure i’ll have more questions.LOL. catcha later.

…or indeed firefox :stuck_out_tongue:

I’ve been using this for a long time.


javascript:q=document.getSelection();if(!q)void(q=prompt('PHP man:',''));if(q)location.href='http://www.php.net/'+escape(q)

Save it as a bookmark(the code is the “location”) and then then add it to your personal bar in opera or firefox. Clicking the button will prompt you to enter something to search php.net for. The gem about it is if you have some text selected on the web page, and then click the button, it will use the selected text to search and bypas the prompt. It’s easily adapted to other websites too.