NewSourceMedia on Facebook

Split() by newline and return using PHP

Share Bookmark and Share

How to create an array by using the split() function on multiple newlines and returns.

Introduction:

I was having a problem trying to split a string of multiple lines from an online form using the flowing code:

<?php
   $arry_names = split("\n", $names);
?>

This was producing an unexpected line break. I searched the web to see if anyone had the same problem and found that I needed to check for “returns” (\r) also. So now when I’m splitting data by lines, I use the following code:

<?php
   $arry_names = split("[\n|\r]", $names);
?>

Please rate this post by clicking a star:

(11 votes, average: 4.91 out of 5)

4 Responses to “Split() by newline and return using PHP”

  • I think it's enough using split("[\n]",$names);

  • Fred

    split (“[\r\n|\n]“, $array); is the solution!

  • what about

    split(‘[\r\n|\r|\n]‘,$array)

    ?

    Anyway, thanks for your help! I really appreciate.

  • Sorry,

    php says:

    This function [split] has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

    Use preg_split instead.

    greets.

Leave a Reply





Tags: , , , , , , , , ,