RegExp and new line

Hello,

Trying to get rid of <ul> inside <p>


$input = preg_replace('/<p>([.\
]*)<ul>/','<ul>',$input);

My regexp works rather fine, but in some cases, it misses a hit. Is it due to new lines? I watched the source code, and there’s really nothing I can spot that could be responsible for this.

:slight_smile:

Could it be because of a greediness issue?

have you tried this regex in a regex tool or another language?

I think that you need something like the following

<?php
$input = "<p>test
<ul>
<li>foo</li>
</ul>
test2
</p>";

$input = preg_replace('#(<p>.+?)<ul>.*?</ul>(.*?</p>)#si','${1}${2}',$input);
$input = preg_replace('#[\\r?\
]{2,}#', "\
", $input);
echo $input;