natasha_thomas Posted March 13, 2011 Share Posted March 13, 2011 Folks, I want to find a Word in a string. If the word is found, i want to retrun TRUE else FALSE. $string: Sol 3 Drawer Chests Antique Pine Bedside Chest $word to be found: Chest Now the tricky part is: In the $string the $word occurs twice once its CHESTS (i mean the first occurance) then CHEST. If its found, i want to return TRUE else FALSE. I tried using strpos() but it just checks for CHEST and not CHESTS. How can it be achieved? Cheers Natasha Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/ Share on other sites More sharing options...
litebearer Posted March 13, 2011 Share Posted March 13, 2011 do you want it to find only the exact match ie Chest NOT chests or chest (case sensitive)? or any match ie chest, Chest, ChEstNuts? Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187016 Share on other sites More sharing options...
natasha_thomas Posted March 13, 2011 Author Share Posted March 13, 2011 any match ie chest, Chest, ChEstNuts? Yes, i want to match any kind of match irrespective of Case. Chest, Chests chestnut, whitechest so on..... how can we achieve this? Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187017 Share on other sites More sharing options...
PaulRyan Posted March 13, 2011 Share Posted March 13, 2011 strstr() Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187018 Share on other sites More sharing options...
cunoodle2 Posted March 13, 2011 Share Posted March 13, 2011 The strpos function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. $string = "Sol 3 Drawer Chests Antique Pine Bedside Chest"; $word = "Chest"; $pos = strpos(strtolower($string), strtolower($word)); // Note our use of ===. Simply == would not work as expected // because the position of 'chest' MIGHT be the 0th (first) character. if ($pos === false) { //the string was not found } else { //the string WAS found } Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187019 Share on other sites More sharing options...
litebearer Posted March 13, 2011 Share Posted March 13, 2011 Try... <?PHP $haystack = "Sol 3 Drawer Chests Antique Pine Bedside Chest"; $needle = "chest"; if(substr_count(strtoupper($haystack), strtoupper($needle))>0){ $found = TRUE; }else{ $found = FALSE; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187020 Share on other sites More sharing options...
natasha_thomas Posted March 13, 2011 Author Share Posted March 13, 2011 Each one of you guys here are so wonderful and supportive. Certianly lot better than Stackoverflow... Thaks to all: stristr() did the job. Cheers Natasha Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187024 Share on other sites More sharing options...
spaceman12 Posted March 13, 2011 Share Posted March 13, 2011 in ur case, stristr() is so very unreliable. Used preg_match() for an absolute error free. It is multifold reliable than the former. Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187088 Share on other sites More sharing options...
natasha_thomas Posted March 14, 2011 Author Share Posted March 14, 2011 in ur case, stristr() is so very unreliable. Used preg_match() for an absolute error free. It is multifold reliable than the former. Helllo, In what sense is it not reliable, i sit no case Insensitive? If so, could you tell me what preg_match() query i need to use to achieve my requirement? Cheers Natasha Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187256 Share on other sites More sharing options...
spaceman12 Posted March 14, 2011 Share Posted March 14, 2011 preg_match() function in php i a regular search expression. For details of its usage, please visit http://www.php.net Here is a sample of code that i made which i believe will be useful to you FOR CASE SENSITIVE SEARCH <?php $string="Sol 3 Drawer Chests Antique Pine Bedside Chest"; $word='Chest'; if(preg_match("*$word*",$string)) { echo $word." "."found"; } else echo $word." ".'not forund'; ?> FOR CASE-INSENSITIVE SEARCH <?php $string="Sol 3 Drawer Chests Antique Pine Bedside Chest"; $word='chest'; if(strcasecmp($word,$string)) { echo $word." "."found"; } else echo $word." ".'not forund'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187264 Share on other sites More sharing options...
QuickOldCar Posted March 14, 2011 Share Posted March 14, 2011 And why not use preg_match with boundary which is the \b and the i for insensitive search? untested, but it should work <?php function matchWord($word,$string){ if (preg_match("/\$word\b/i", $string)) { $the_result = "match"; } else { $the_result = "no match"; } return $the_result; } //usage $text="Sol 3 Drawer Chests Antique Pine Bedside Chest"; $the_word = "Chest"; $is_it_there = matchWord($the_word,$text); echo $is_it_there; ?> Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187267 Share on other sites More sharing options...
QuickOldCar Posted March 14, 2011 Share Posted March 14, 2011 well i didn't escape the last variable, but I'm guessing don't want the boundaries so left it out <?php function matchWord($word,$string){ if (preg_match("/$word/i", $string)) { $the_result = "match"; } else { $the_result = "no match"; } return $the_result; } //usage $text="Sol 3 Drawer Chests Antique Pine Bedside Chest"; $the_word = "Chest"; $is_it_there = matchWord($the_word,$text); echo $is_it_there; ?> Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187274 Share on other sites More sharing options...
natasha_thomas Posted March 15, 2011 Author Share Posted March 15, 2011 Thanks everyone. Thanks Quicky. Another thing, i used this nice little monster to singularize the plural along with the codes you guys supplied, for enhancement. <?php function depluralize($word){ // Here is the list of rules. To add a scenario, // Add the plural ending as the key and the singular // ending as the value for that key. This could be // turned into a preg_replace and probably will be // eventually, but for now, this is what it is. // // Note: The first rule has a value of false since // we don't want to mess with words that end with // double 's'. We normally wouldn't have to create // rules for words we don't want to mess with, but // the last rule (s) would catch double (ss) words // if we didn't stop before it got to that rule. $rules = array( 'ss' => false, 'os' => 'o', 'ies' => 'y', 'xes' => 'x', 'oes' => 'o', 'ies' => 'y', 'ves' => 'f', 's' => ''); // Loop through all the rules and do the replacement. foreach(array_keys($rules) as $key){ // If the end of the word doesn't match the key, // it's not a candidate for replacement. Move on // to the next plural ending. if(substr($word, (strlen($key) * -1)) != $key) continue; // If the value of the key is false, stop looping // and return the original version of the word. if($key === false) return $word; // We've made it this far, so we can do the // replacement. return substr($word, 0, strlen($word) - strlen($key)) . $rules[$key]; } return $word; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1187607 Share on other sites More sharing options...
kickstart Posted September 11, 2012 Share Posted September 11, 2012 Hi Just a note to say that there is a minor bug in the above function. It checks for a key being false in the array of rules (to cope with a string ending ss), yet it needs to check for the value instead. Minor change to correct this <?php function depluralize($word){ // Here is the list of rules. To add a scenario, // Add the plural ending as the key and the singular // ending as the value for that key. This could be // turned into a preg_replace and probably will be // eventually, but for now, this is what it is. // // Note: The first rule has a value of false since // we don't want to mess with words that end with // double 's'. We normally wouldn't have to create // rules for words we don't want to mess with, but // the last rule (s) would catch double (ss) words // if we didn't stop before it got to that rule. $rules = array( 'ss' => false, 'os' => 'o', 'ies' => 'y', 'xes' => 'x', 'oes' => 'o', 'ies' => 'y', 'ves' => 'f', 's' => ''); // Loop through all the rules and do the replacement. foreach($rules as $key=>$value){ // If the end of the word doesn't match the key, // it's not a candidate for replacement. Move on // to the next plural ending. if(substr($word, (strlen($key) * -1)) != $key) continue; // If the value of the key is false, stop looping // and return the original version of the word. if($value === false) return $word; // We've made it this far, so we can do the // replacement. return substr($word, 0, strlen($word) - strlen($key)) . $rules[$key]; } return $word; } ?> Useful function but this minor issue caught me out. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1377027 Share on other sites More sharing options...
Jessica Posted September 11, 2012 Share Posted September 11, 2012 Did a guru really just necro a thread? (Okay, I've been waiting to use that term for ages now.) Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1377029 Share on other sites More sharing options...
spiderwell Posted September 11, 2012 Share Posted September 11, 2012 what term,guru? Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1377039 Share on other sites More sharing options...
Jessica Posted September 11, 2012 Share Posted September 11, 2012 no, necro. Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1377040 Share on other sites More sharing options...
kickstart Posted September 12, 2012 Share Posted September 12, 2012 Hi Yes, but only dead 6 months. And to correct a minor bug with a useful function that people are likely to find should they google for such a solution All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/230519-finding-word-in-a-string-this-way/#findComment-1377179 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.