I am trying to fix some HTML, and it is working perfect. But one thing is givind me troubles; specifically the open and close <p></p> tags.
I need to fix two cases:
a) Remove the <p> </p> sequences.
b) Remove end and start blankspaces IN paragraphs.
I am doing this:
preg_replace("#<p>\s*</p>#","",$str);
preg_replace("#\s?(</?p>)\s?#", '$1', $str);
But none of both is replacing anything. What am I doing wrong, not tottaly right?
Edit: original code
$source=preg_replace("#\s?(</?p>)\s?#", '$1', $source); //Replace possible innecessary blank spaces
return preg_replace("#<p>\s*</p>#","",$source); //Delete all posible empty pharagraphs
Thank you 😀
Use single quotes for clearer back-slashes. Did you mean \s or \\s? Luckily, in this case it doesn’t matter.
The above PCRE will remove <p> </p> tags that only have whitespace (or nothing) between them, whitespace just after <p> and whitespace just before </p>. I don’t see why your original code wouldn’t have worked, though. You should have removed a single space both before or after any <p> or </p> tag (i.e.
" </p> "would become"</p>".If you’re still having trouble you need to post more code. Are you passing your variable in correctly, etc.? Try to write a minimal case with your problem and post the whole program.