drisate Posted January 30, 2012 Share Posted January 30, 2012 Hey guys i need to find a way to return all the phrases a certain word is found in. Ex: $str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In facilisis faucibus vehicula. Praesent fermentum odio at dolor semper rutrum. Etiam pretium, diam ac mattis vehicula, dui ipsum congue ipsum, in sodales neque libero sit amet lorem. Morbi ac neque nec arcu feugiat convallis. Praesent quis diam a libero varius ipsum elementum."; $phrases = find_phrase($str, "ipsum"); $phrases would then output an array like this: $phrases[0] = Lorem ipsum dolor sit amet, consectetur adipiscing elit. $phrases[1] = Etiam pretium, diam ac mattis vehicula, dui ipsum congue ipsum, in sodales neque libero sit amet lorem. $phrases[2] = Praesent quis diam a libero varius ipsum elementum. How would the find_phrase function be build to achieve that? Quote Link to comment https://forums.phpfreaks.com/topic/256052-return-all-the-phrases-a-word-is-found-in/ Share on other sites More sharing options...
JonnoTheDev Posted January 30, 2012 Share Posted January 30, 2012 <?php function find_phrase($str, $match) { $parts = explode(".", $str); $matches = array(); foreach($parts as $s) { if(strstr($s, $match)) { $matches[] = $s; } } return $matches; } $str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In facilisis faucibus vehicula. Praesent fermentum odio at dolor semper rutrum. Etiam pretium, diam ac mattis vehicula, dui ipsum congue ipsum, in sodales neque libero sit amet lorem. Morbi ac neque nec arcu feugiat convallis. Praesent quis diam a libero varius ipsum elementum."; $phrases = find_phrase($str, "ipsum"); print "<pre>"; print_r($phrases); print "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/256052-return-all-the-phrases-a-word-is-found-in/#findComment-1312647 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.