Jump to content

Find First Sentence without including decimals


brooksh

Recommended Posts

I want to find the first sentence in a string without counting decimals. Here is the code I have, but I still want it to display the decimals, but I don't want it to count as the end of a sentence.

 

$content = "This is my test string which is 3.5% of what I'm trying to do. This is only a test.";

 

function first_sentence($content)
{ 
if(preg_match('/[0-9]+\.[0-9]/', $content, $num)) {
$number = "$num[0]";
$content = ereg_replace("$number","", $content);
} 
preg_match('/^.*[^\s](\.|\?|\!)/U', $content, $match); 
$string = $match[0];

$title_count = strlen($match[0]);
if($title_count > "6"){
return "$match[0]"; 
}
else {
$content2 = ereg_replace("$match[0]","", $content); 
preg_match('/^.*[^\s](\.|\?|\!)/U', $content2, $step); 
return "$match[0] $step[0]"; 
}}

 

Currently this is what it displays: This is my test string which is % of what I'm trying to do.

This is what I want it to display: This is my test string which is 3.5% of what I'm trying to do.

Unfortunately, computers suck at interpretation. A human can easily see that the period in "3.5" is not the end of the sentence. Now, that's not to say you can't make a better pattern. For example, to determine the end of a sentence you should be looking for a period followed by a space or the end of the text. That's the best pattern I can think of. There could be plenty of valid scenarios where that logic would fail. You either have to make more complex patterns or live with the limitations.

 

I don't think you need regular expression at all. IN fact, you are using deprecated function. Try this

$firstSentence = substr($text, 0, strpos($text, '. '))

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.