How to replace a domain inside a link using preg_replace

Discussion in 'PHP' started by kaene, Nov 1, 2008.

  1. #1
    Hi, I'm trying to replace a domain in a text string, I'm using preg_replace but I can't find the right pattern

    I tried:

    $reg_xpr = "/(<a.* href=?)(.*?)(>?)(<\/a>?)/";
    $repl = "$1www.newdomain.com$3$4$5";
    $body = preg_replace($reg_xpr, $repl, $body);


    and it will work for links like this:
    $body = '<a href="http://www.domain.com">Link</a>';

    and this:
    $body = '<a href='http://www.domain.com'>Link</a>';

    but I don't know how to make it work for this
    $body = '<a href=http://www.domain.com>Link</a>';

    I'm new working with regular expression, so I hope you don't blame me :eek:

    Thanks!
     
    kaene, Nov 1, 2008 IP