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
https://forums.phpfreaks.com/topic/49836-print-only-first-sentence/
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.'
?>

 

 

 

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.