Strings

How to split a string in PHP from an array based on the new line characters

Sometimes a string, especially when it is submitted through a textarea, is not easy to be separated based on its paragraphs. When a user for example hits the Enter button in a textarea then we probably need to add the necessary <p> and </p> before and after each section of the submitted text. Using the preg_split('/\r\n|\r|\n/', $text)  you can recognise the new lines contained in the $text variable. As a result you can easily loop over the array and handle each row separately.

Here is an example where a PHP string containing a text with line breaks can be split into an array.