PHP Code to convert text in array of lines and use specific line
Posted On at by milanWhile parsing with PHP some times we need to parse the whole text to find and use only a specific line. In this senario converting the whole text in array of lines will be a really handy task. And easily we can reach that specific line.
Now text my be of two types, one coming from a text file etc or coming from html file following are two methods may be used to make array of lines from the given text.
// Consider variable $text contains some text ( one or more paragraphs )
//Normal text / Text coming from text file'
$textAry = explode("\n", $text);
?>
//HTML Text ( remeber in it new line comes with a
)
$textAry = explode("
", $text);
?>
Now array of lines will reside in an array variable $textAry
And if you have to use the fifth line from array you will use it like:
echo $textAry[4]; //indexing starts from 0 :-)
Also you can use this array according to your requirements, I mean you can search in array or use any other way you want and do wonders. :-)