agahi agahi - 4 months ago 8
PHP Question

removing unknown number of tags from inside a tag

I have these string:

$st = '<p><br/></p>';
$st = '<p><br/><br/></p>';
$st = '<p><br/><br/><br/></p>';


How can I remove all the line breaks from inside the P tag?
I tried this but didn't work:

echo preg_replace('/p>(br\/>)*?<\/p/','p></p',$st);


I'm sorry, seem I need to explain. more. This would be inside a larger text and I don't want to remove all the line breaks. Only those which are inside a P tag with unknown number of occurance.

Answer

Change your REGEX to:

preg_replace('#p\>(\<br\/\>)*?\<\/p#','p></p',$st)

Comments