1

I'm trying to trim string in quite non-standard way. The text is devided on two parts - visible teaser and hidden rest part which is showing by JS after clicking a link. I need somethink like that: My text:

Teaser: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas enim est, convallis non ullamcorper hendrerit, vehicula ac quam. In malesuada suscipit eleifend. Rest: Donec posuere ipsum at ipsum feugiat eu ornare nunc ultricies. Donec eleifend odio a eros semper cursus. Donec posuere ipsum at ipsum feugiat eu ornare nunc ultricies. Donec eleifend odio a eros semper cursus.

My problem is because all the text is stored in single field in database. In need to trim teaser after dwo dots or 100 characters and then show the rest part of text (whithout loosing any character). The "rest" part must start with new sentence and big letter like in above example.

If I would use substr on the text the rest will start with random letter...

$text = '';
$text = strip_tags($row['text']);
$text = explode('.', $text);
$teaser = $text[0].$text[1].'.';
$cut = strlen($teaser);
if($cut > 100) 
{
    $teaser = trimString($text, 0, 100);
    $cut = 100;
}
$theRest = substr($row['text'], $cut);

With this I have teaser ending with "." or "..." (my function trimString works like that) and this is good. But the rest in this scenario starts with random letter and this is not what i want. I'm afraid that someone would use dot in the middle of sentence which will cause another problems. Any ideas?

webrama.pl
  • 1,870
  • 1
  • 23
  • 36
  • Could you not cut the teaser from the main text so that the teaser is just a snippet and the main text is in it's entirety? Teaser: Oh look at this block... Main: Oh look at this block of text it's lovely – Dale Apr 09 '13 at 10:16
  • start from the 100th letter looking backswards for the first capital letter. if you find it go back 1 or 2 letters and look if that is a dot. if it is then you have your substr limit position for your teaser – ITroubs Apr 09 '13 at 10:16
  • 1
    I don't think it's that simple. Do you really mean "2 dots" or do you mean "2 sentences"? What if you don't have any dots but have exclamation marks instead? Also looking for capital letters isn't the best idea either: what if you have names in the text, or months, cities, etc.? – Aleks G Apr 09 '13 at 10:18
  • Yeah I can't think of any easy way to do this without looping throught the text – aleation Apr 09 '13 at 10:22
  • Aleks, you hit the nail on the head. I want two sentences in teaaser. Any ideas how to slove this? – webrama.pl Apr 09 '13 at 10:27

1 Answers1

0
$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas enim est, convallis non ullamcorper hendrerit, vehicula ac quam. In malesuada suscipit eleifend. Donec posuere ipsum at ipsum feugiat eu ornare nunc ultricies. Donec eleifend odio a eros semper cursus. Donec posuere ipsum at ipsum feugiat eu ornare nunc ultricies. Donec eleifend odio a eros semper cursus.';

$cut = strpos($string, '. ', 100)+1;
$teaser = substr($string, 0, $cut);
$rest = substr($string, $cut);

Explanation: Used strpos() to search the position of the first ocurrence of '. ' (as new sentence start).

I used the offset argument(100) to start searching from position 100. Added +1 to the offset so it shows the dot.

So $new_cut is the position calculated with strpos() to use with substr()


The Only problem with this is that you can't use multiple needles, I mean for searching at same time for '...', there's another post on SO that might help you to solve this issue and combine it with my answer:

strpos() with multiple needles?

Community
  • 1
  • 1
aleation
  • 4,796
  • 1
  • 21
  • 35