dagnasty Posted July 25, 2006 Share Posted July 25, 2006 I've seen it before, I just cannot remember the function.say I have an array of words:catdogfishbucketcheesewheeland 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. Quote Link to comment https://forums.phpfreaks.com/topic/15571-php-function-to-find-string-within-a-string-from-array-of-strings/ Share on other sites More sharing options...
zq29 Posted July 25, 2006 Share Posted July 25, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/15571-php-function-to-find-string-within-a-string-from-array-of-strings/#findComment-63317 Share on other sites More sharing options...
dagnasty Posted July 25, 2006 Author Share Posted July 25, 2006 undefined function stripos(); :( My guess is that this function is only for PHP 5?Do I have an alternative function to use? Quote Link to comment https://forums.phpfreaks.com/topic/15571-php-function-to-find-string-within-a-string-from-array-of-strings/#findComment-63335 Share on other sites More sharing options...
dagnasty Posted July 25, 2006 Author Share Posted July 25, 2006 strpos(strtolower($str))nevermind I'll settle with this. Quote Link to comment https://forums.phpfreaks.com/topic/15571-php-function-to-find-string-within-a-string-from-array-of-strings/#findComment-63340 Share on other sites More sharing options...
wildteen88 Posted July 25, 2006 Share Posted July 25, 2006 stripos is a PHP5 only function, you must be using PHP4 or lower. So you'll have to use strpos Quote Link to comment https://forums.phpfreaks.com/topic/15571-php-function-to-find-string-within-a-string-from-array-of-strings/#findComment-63391 Share on other sites More sharing options...
Barand Posted July 25, 2006 Share Posted July 25, 2006 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 resultsecho '<pre>', print_r($found, true), '</pre>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15571-php-function-to-find-string-within-a-string-from-array-of-strings/#findComment-63550 Share on other sites More sharing options...
zq29 Posted July 25, 2006 Share Posted July 25, 2006 [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 resultsecho '<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... Quote Link to comment https://forums.phpfreaks.com/topic/15571-php-function-to-find-string-within-a-string-from-array-of-strings/#findComment-63579 Share on other sites More sharing options...
Barand Posted July 25, 2006 Share Posted July 25, 2006 Good point Quote Link to comment https://forums.phpfreaks.com/topic/15571-php-function-to-find-string-within-a-string-from-array-of-strings/#findComment-63581 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.