Results 1 to 2 of 2

Thread: regular expressions

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    regular expressions

    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:
    Code:
    <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):

    Code:
    $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!

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: regular expressions

    You need to escape your forward slashes in the $pattern:
    Code:
    $pattern = '/<li><a href="[^>]*">'.$row["name"].'<\/a><\/li>/';

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width