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
$mytext="Line1
Line2
Line3
....."
keeptwentyline($mytext);
?>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I suppose a (maybe a bit dumb ^^ ) solution would be to :
explodethe string into an array of linesarray_sliceimplodethose back into a string.Something like that would correspond to this idea :
Note : I passed the number of lines as a parameter — that way, the function can be used elsewhere 😉
And if the parameter is not given, it takes the default value : 10
But there might be some clever way ^^
(that one will probably need qiute some memory, to copy the string into an array, extract the first lines, re-create a string…)