dc_jt Posted May 3, 2007 Share Posted May 3, 2007 Hi I have the following: <?=strip_tags(substr($oTeam->description,0,140))?><?=(strlen($oTeam->description) > 140) ? '...' : ''?> However, this displays the first 140 characters. I want to be able to display only the first sentence, basically everything upto the first full stop or question mark etc. Any idea how? Thanks Quote Link to comment Share on other sites More sharing options...
Nameless12 Posted May 3, 2007 Share Posted May 3, 2007 use an offset of 140 on a few strpos() calls that are looking for fullstops and question marks. After that just check which one is shorter and use that value with substr(). Quote Link to comment Share on other sites More sharing options...
dc_jt Posted May 3, 2007 Author Share Posted May 3, 2007 Got this function: $aPara = 'this is my para? this is the second sent'; $aSent = split('[?!.]',$aPara); print $aSent[0]; But it takes off the [?!.] at the end so its not perfect but might have to do Quote Link to comment Share on other sites More sharing options...
Nameless12 Posted May 3, 2007 Share Posted May 3, 2007 I dont your code for two reasons the first is it returns an array and the second is it will be slower on larger documents. You are only selecting the first sentence in your example but if you are selecting from a document the array will have all the other sentences in them too. This is the implementation of what i described in my previous post. <?php function firstSentence($text) { if ($f = strpos($text, '.')) $a[] = $f; if ($q = strpos($text, '?')) $a[] = $q; if ($x = strpos($text, '!')) $a[] = $x; if ($a) return substr($text, 0, min($a) + 1); } echo firstSentence('some text. here?'); //output 'some text.' ?> Quote Link to comment 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.