View Full Version : Splitting Text

12-22-2005, 08:38 AM
Hi all,

I've got yet another question for you all, this time regarding splitting text with PHP.

I've got a database, with fields full of paragraphs. Each paragraph is seperated with a blank line, no html tag or anything.

The problem with this is when I edit it it all comes out in one chunk. I need each paragraph to be outputted like it is in the database. I've tried replacing /n with <p> in an explode function, but that doesn't seem work, so does anyone have any ideas?


12-22-2005, 09:36 AM
you did it wrong

its \n not /n

12-22-2005, 10:15 AM
Yeah think I tried that as well, still doesn't seem to work though.

$split = explode("\n", "<p>", $allrow[content]);
echo $split[0];

That's what I have, anyone know what is wrong?

Thanks :)

12-22-2005, 10:17 AM
Hey, why am I using explode?

Just done basically the same as above apart from with str_replace and it works.

Don't have a clue why I used explode originally, heh, problem solved :)

12-22-2005, 11:06 AM
Your syntax for explode is wrong.. you are using 2 params, should only be one:

$split = explode("\n", "<p>", $allrow[content]);
echo $split[0];

Should be:

$split = explode("\n", $allrow[content]);
echo $split[0];

I think that's where you went off book ;)

12-22-2005, 11:11 AM
Yeah haha :p

I got the two completely mixed up, hehe...

The New Guy
12-22-2005, 11:33 AM
You might find this useful

function nl2p ($text){
$text = '<p>' . $text . '</p>';
$text = str_replace("\n",'</p><p>',$text);
$text = str_replace("\r",'',$text);
$text = str_replace('<p></p>','',$text);
$text = str_replace('</p><p>', "</p>\n<p>", $text);

return $text;

12-22-2005, 01:31 PM
TNG - Good function. I like nl2br and this really compliments that.

12-22-2005, 01:58 PM
I second that. Excellent function, quite useful for many uses.