brooksh Posted October 10, 2011 Share Posted October 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248835-find-first-sentence-without-including-decimals/ Share on other sites More sharing options...
jcbones Posted October 10, 2011 Share Posted October 10, 2011 So, you need to find ONLY the decimal that has a space after it? You will still have to think about abbreviations. Quote Link to comment https://forums.phpfreaks.com/topic/248835-find-first-sentence-without-including-decimals/#findComment-1277888 Share on other sites More sharing options...
Psycho Posted October 10, 2011 Share Posted October 10, 2011 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, '. ')) Quote Link to comment https://forums.phpfreaks.com/topic/248835-find-first-sentence-without-including-decimals/#findComment-1277906 Share on other sites More sharing options...
brooksh Posted October 10, 2011 Author Share Posted October 10, 2011 I guess I just over complicated things. Of course putting a space after the "." works. Quote Link to comment https://forums.phpfreaks.com/topic/248835-find-first-sentence-without-including-decimals/#findComment-1277920 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.