PEHLA KALMA

PEHLA KALMA
Sanaullah Sajid

How to limit lines in a string php

Questions:

I have a variable like the following and i want a function to only keep the first 20 lines, so it will strips any additional \n lines more than 20?
PHP how to limit lines in a string?
How to fixed line in php?
How to fixed X line like facebook style?

Change text length in php and provide Read more link



Ans:
< ?PHP 
//function start for line limit
function Lines($strs, $num=10) {
    $lines = explode("\n", $strs);
    $firsts = array_slice($lines, 0, $num);
    return implode("\n", $firsts);
}
//function off
//$text = '......'
$text="line1
line2
line3
line4
line5
line6
";

 $val = lines($text,4);//4 is limit line x
echo nl2br($val);//n2br for show lines
echo "..Read More";
? >