-
Splitting Text
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?
Cheers,
Mike
-
you did it wrong
its \n not /n
-
Yeah think I tried that as well, still doesn't seem to work though.
PHP Code:
$split = explode("\n", "<p>", $allrow[content]);
echo $split[0];
That's what I have, anyone know what is wrong?
Thanks :)
-
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 :)
-
Your syntax for explode is wrong.. you are using 2 params, should only be one:
Code:
$split = explode("\n", "<p>", $allrow[content]);
echo $split[0];
Should be:
Code:
$split = explode("\n", $allrow[content]);
echo $split[0];
I think that's where you went off book ;)
-
Yeah haha :p
I got the two completely mixed up, hehe...
-
You might find this useful
PHP Code:
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;
}
-
TNG - Good function. I like nl2br and this really compliments that.
-
I second that. Excellent function, quite useful for many uses.