Jump to content

Recommended Posts

Ok - think I got it.  This relies on always finding a period at the end of every sentence.

$contents = "This is where cat is found.  And it may be where cat is found a second time.  And here it is the third occurrence of cat.  Following the 3rd cat we have the 4th cat right here.";
$sch_str = 'cat';

echo "Contents are:<br><br>";
echo "$contents<br><br>";
$last_start = 0;
$last_end = 0;
while ($pos = stripos($contents, $sch_str))
{
	// we have found the pos of the desired string
	// now locate the . prior to that pos
	$start = strrpos(substr($contents, 0, $pos), '.');
	if (!$start)
		$start = 0;
	// now find the . following the current pos
	$end = stripos(substr($contents, $pos), '.') + $pos +1;
	// now output the sentence using start and end pos's
	echo "start $last_start, end " . 
			($last_end + $end) . " : " . 
			substr($contents, $start, $end) . 
			'<br>';
	$contents = substr($contents, $end + 1);
	$last_start = $last_start + $end +1;
	$last_end = $last_end + $end +1;
}

 

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.