Posted by: prajapatinilesh | October 29, 2008

Split() by newline and return using PHP

I was having a problem trying to split a string of multiple lines from an online form using the flowing code:
$namesArr = split(“\n”, $names);

This was producing an unexpected line break. later i found that I needed to check for “returns” (r) also. So now when I’m splitting data by lines, I used the following code:

namesArr = split(“[\n|\r]”, $names);

This is also very good function of php (inbuilt function)

= "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");

Or – last argument passed as 1.

= "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", 1);



  1. Ok… why now all of a sudden this code is now splitting the letter ‘n’ instead of a new line

    • Hey,

      You need to pass “\n” means \n in double quotes. same way for \r

  2. You have to use

    Better luck 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: