i want to remove all the text after the last occurring of " . " in a given paragraph.
For example from this paragraph i want to remove all text after last full stop.
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making开发者_Go百科 it over 2000 years old. Rich
Length of the paragraph is dynamic only thing to be removed is text after last " . "
You can do this with strrpos
and substr
:
$pos = strrpos($paragraph, '.');
$withOutDot = substr($paragraph, 0, $pos). '.';
Maybe something like this:
new_string = substr(old_string, 0, strchr(old_string, '.'));
strchr
finds the position of the last occurrence of '.' in old_string
. substr
returns a new string consisting of the characters in old_string
between positions 0 and the position found by strchr
.
- http://uk.php.net/manual/en/function.substr.php
- http://uk.php.net/manual/en/function.strrchr.php
For me, the above solutions didn't work out so finally i got some code by myself so it may help to other people.
<?php
$trim_content = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Rich";
$place = strripos($trim_content,".");
$content = substr($trim_content,$place);
echo str_replace($content,".",$trim_content);
?>
You can do it with explode.
$summary = explode(".",$summary);
$summary=$summary[1]
here you will get text till the first full stop
精彩评论