PDA

View Full Version : Help, tricky preg_replace


MrNase
Sun 21st Nov '04, 7:51am
Hello,

I need a regular expression to do the following thing:

Replace
<ul><li>test</li><li>test</li><li>test</li></ul>
with:
test, test, test

Replace
<ul><li>test</li></ul>
with
test

Replace
<ul><li>test</li><li>test</li></ul>
with
test, test


I hope you got what I mean :)
The difficult thing is I don't know how long that grown :/
It may even be something like

<ul><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li></ul>

jumpD
Sun 21st Nov '04, 2:27pm
You don't realy need the overhead of preg to do that.


$string = '<ul><li>test</li><li>test</li><li>test</li></ul>';
$replaced = array('<ul>','</ul>','<li>','</li>');
$replace = array('','','',', ');
$new_string = rtrim(str_replace($replaced, $replace, $string), ', ');
echo $new_string;