PDA

Click to See Complete Forum and Search --> : regular expressions


Arsenal
Jul 9th, 2009, 03:23 AM
Hi,

i'm developping a php-based site, and I'm stuck on the following;

I have sever links placed in a list like this:

<ul>
<li class="active">Home</li>
<li><a href="whatever.php">whatever</a></li>
<li><a href="whatever.php?id=3623">anotherwhatever</a></li>
....
</ul>

Now, i want to find the right <li>....</li> line that contains the name (whatever, anotherwhatever, home...) so i can replace it with <li class="active>whatever</li>.

I know I need to use preg_replace for this (since you can't simply use a wildcard with str_replace, en de href attrib isn't constant), But my Regular expression skills seem to be lacking.

This is what I've got (and obviously doesn't work):


$pattern = '/<li><a href="[^>]*">'.$row["name"].'</a></li>/';
$template = preg_replace($pattern, "<li class='active'>".$row['name']."</li>", $template);
echo $template;



Any help would be muchly appreciated!

SambaNeko
Jul 9th, 2009, 09:37 AM
You need to escape your forward slashes in the $pattern:

$pattern = '/<li><a href="[^>]*">'.$row["name"].'<\/a><\/li>/';