Jump to content


Photo

PHP function to find string within a string from array of strings


  • Please log in to reply
7 replies to this topic

#1 dagnasty

dagnasty
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 25 July 2006 - 11:24 AM

I've seen it before, I just cannot remember the function.

say I have an array of words:

cat
dog
fish
bucket
cheese
wheel

and am looking in a bigger string to see if any of them exist (not case sensitive) within a string:

"the monkey jumped from the bucket"

What was that function name? Thanks.

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 25 July 2006 - 11:36 AM

I can't think of just one function that does this, I know a handfull exist for replacements, but not searches, well, none come to mind. YOu could do it like this though:
<?php
$words = array("cat","dog","fish","bucket","cheese","wheel");
$str = "the monkey jumped from the bucket";
$found = array();
foreach($words as $w) {
    if(stripos($str,$w) !== FALSE) $found[] = $w;
}
?>


#3 dagnasty

dagnasty
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 25 July 2006 - 12:15 PM

undefined function stripos(); :(

My guess is that this function is only for PHP 5?

Do I have an alternative function to use?



#4 dagnasty

dagnasty
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 25 July 2006 - 12:28 PM

strpos(strtolower($str))

nevermind I'll settle with this.

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 July 2006 - 02:06 PM

stripos is a PHP5 only function, you must be using PHP4 or lower. So you'll have to use strpos

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 25 July 2006 - 06:40 PM

Alternatively, if you convert the sentence to an array of words

<?php
$words = array ('cat','dog','fish','bucket','cheese','wheel');
$words2 = explode(' ', strtolower("the monkey jumped from the bucket"));
$found = array_intersect($words, $words2);

// view results
echo '<pre>', print_r($found, true), '</pre>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 25 July 2006 - 07:16 PM

Alternatively, if you convert the sentence to an array of words

<?php
$words = array ('cat','dog','fish','bucket','cheese','wheel');
$words2 = explode(' ', strtolower("the monkey jumped from the bucket"));
$found = array_intersect($words, $words2);

// view results
echo '<pre>', print_r($found, true), '</pre>';
?>

Yeah, that was one of the other ways I was thinking too. If you go about it this way you'll have to strip out any punctuation if it's present in your actual data though...

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 25 July 2006 - 07:20 PM

Good point
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users