I've got a form with a textarea and I want to change the linebreaks from the input into paragraphs (using the <p>
tag). I'm using explode and implode to replace \n
with </p><p>
, but if you have two line breaks in a row, you end up with </p><p></p><p>
. I'm trying to use a foreach loop to go through each element and check if it it's empty to combat this, but it doesn't seem to be working at all. Here's what I have:
foreach($text as $value){
if($value!=''){
$newtext.='</p><p>'.$value;
}
}
That still gives </p><p></p><p>
for things that are double spaced. I also tried replacing if($value!='')
with !is_null(trim($value))
and it still didn't work. What is wrong with my code, and how can I fix it?
Something like this will work:
$newtext = '<p>' . implode('</p><p>', array_filter(explode("\n", $textarea))) . '</p>';
\n
p
tagsIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With