Jump to content

Recommended Posts

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.
I can't think of [i]just one[/i] 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:
[code]<?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;
}
?>[/code]
Alternatively, if you convert the sentence to an array of words

[code]<?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>';
?>
[/code]
[quote author=Barand link=topic=101778.msg403339#msg403339 date=1153852857]
Alternatively, if you convert the sentence to an array of words

[code]<?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>';
?>
[/code]
[/quote]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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.