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:
<?php
$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:

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

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

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

Or – last argument passed as 1.

<?php
$text
= "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", 1);
echo
"$newtext\n";
?>

Advertisements

Responses

  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 🙂
    –Govind


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: