Jump to content

Print only first sentence


dc_jt

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.'
?>

 

 

 

Link to comment
Share on other sites

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.