views:

24

answers:

2

This function doesn't work:

function remove_ul($ul) {
    $ul = preg_replace('/<ul id="general-nav">/', '<ul class="nav">', $ul, 1);
    $ul = preg_replace('/</ul>/', '<li class="blank"></li></ul>', $ul, 1);
    return $ul;
}

I think because of repeating / on line with </ul>

Any idea?

Thanks.

+3  A: 

Try replacing this:

$ul = preg_replace('/</ul>/', '<li class="blank"></li></ul>', $ul, 1);

with:

$ul = preg_replace('/<\/ul>/', '<li class="blank"></li></ul>', $ul, 1);

Or try:

$ul = preg_replace('#</ul>#', '<li class="blank"></li></ul>', $ul, 1);

Because in your code, you have specified the delimiter / and then using </ul>, there is conflict, you need to either escape the delimiter with \ or use # as delimiters.

Sarfraz
+1  A: 

Escape the slash in the second expression (/<\/ul>/). If your query becomes more complex, you might have to use a capturing group as well (parentheses).

Oh and parsing html with regex is evil. In before the XHTML Regex bandwagon storms this post. :)

Denis 'Alpheus' Čahuk