Jump to content

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, '. '))

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.