Jump to content

Recommended Posts

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?

<?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>";
?>

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.