Split PHP string

I found this php script:

// separate the tags so as not to break any html $ string = strip_tags ($ string);

if (strlen ($ string)> 500) {

// truncate string
$stringCut = substr($string, 0, 500);

// make sure it ends in a word so assassinate doesn't become ass...
$string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="/this/story">Read More</a>';  } echo $string;

create webbiedave. Now my question is, how can I tell strrpos to search in space or point?

So, if I limit the line to 22, and if I had something like StackOverFlow - the best site ever → would output StackOverFlow - this ... and if I had something like /qaru.site/ ... - the best site, ever - the output will be only ... because it did not find a space and a length of the string> 22. How can I change this script to cut out the text if I find a point in string /qaru.site/ ... - the best site → become http: // stackoverflow ...?

+3
3

, . , , '!' . cond - , +1 . , . (- /[: alfa] + $/, , ).

, , .

, :

function word_wrap_custom($string, $limit){
$oneWord = explode(' ', $string);
if(count($oneWord) < 2 )
    return $oneWord[0];
$string = substr($string, 0, $limit + 2);

$endchar =  substr($string, $limit, $limit + 1);

$postendchar = substr($string, $limit + 1, $limit + 2);

$arrAccetpEndChar = array(' ', '!', '?', ',', '.', ';', ':');

if(in_array($postendchar, $arrAccetpEndChar) || in_array($endchar, $arrAccetpEndChar))
{
    return $string;
}
else
{
    return preg_replace('/[A-Za-z0-9]+$/', '', $string);
}
}
+1

, , , , wordwrap .

$longText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ut purus a tellus ultrices vulputate. Aliquam posuere facilisis elit ut adipiscing. Nunc auctor dignissim porta. Vestibulum vitae tempor augue. Nam vel odio quis quam gravida ultrices sed a arcu. Phasellus nec odio massa. Duis imperdiet rutrum mi, vitae volutpat nulla convallis quis. Donec dignissim pulvinar mauris id molestie. Duis id mauris augue, id sagittis velit. Ut justo lectus, scelerisque egestas tempor et, facilisis vitae erat. Quisque ut mattis nulla. Donec a justo quis nisi tempus ultrices. Phasellus non dui non dolor tristique tincidunt vitae imperdiet libero. Pellentesque pretium luctus sem.";

$makeLine = wordwrap( $longText, 50, PHP_EOL );

echo $makeLine;

: http://codepad.org/Dqz8qzAy

, , , :

$longText = "...";

$makeLine = wordwrap( $longText, 50, '\r\n' );
$firstSen = array_shift( explode( '\r\n', $makeLine ) );

echo $firstSen; // Lorem ipsum dolor sit amet, consectetur adipiscing...
+2

This will not interrupt the words:

function ShortenText($text, $chars)
{
    $chars = $chars;$text = $text." ";
    $countchars = strlen($text);
    if($countchars > $chars)
    {
        $text = substr($text,0,$chars);
        $text = substr($text,0,strrpos($text,' '));
        $text = $text."...";
    }
    return $text;
}
0
source

All Articles