Regular expression to replace HREFs with #

Discussion in 'PHP' started by 3dom, Dec 27, 2007.

  1. #1
    Hello!

    Need regular expression to replace all HREFs in A tags with # - all I found on php.net was

    eregi_replace('<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>].*>','<a href="#">', $string);

    which doesn't work.

    Are there any ready-to-go solutions?

    Thanks
    =)
     
    3dom, Dec 27, 2007 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    You want to change the actual link or change the href part?
     
    HuggyStudios, Dec 27, 2007 IP
  3. 3dom

    3dom Peon

    Messages:
    304
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I want to replace URLs in links with # symbol but save additional attributes of A tags.
     
    3dom, Dec 27, 2007 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    Try this,

    
        $var = preg_replace("/\s([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*
                \@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})([\s|\.|\,])/i",
                " <a href=\"$1\">", $var);
    
    PHP:
     
    HuggyStudios, Dec 27, 2007 IP
  5. 3dom

    3dom Peon

    Messages:
    304
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Exactly opposite thing needed: from
    
    <A href="link_here" class="someClass" target="_someTarget">
    
    Code (markup):
    I need to make
    
    <A href="#" class="someClass" target="_someTarget">
    
    Code (markup):
    in full page's code (in simple words I need PHProxy to replace all clickable A tags into # anchors)
     
    3dom, Dec 27, 2007 IP
  6. pratham

    pratham Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    preg_replace ("/href=['\"][^'\"]*['\"]/i", "href='#'", $url);
    PHP:
     
    pratham, Dec 28, 2007 IP
  7. 3dom

    3dom Peon

    Messages:
    304
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks but this expression going to rewrite Link/Style tags also (<link rel="style" href="some_href") - need to replace only A tags. Getting errors (like everything replaced with #) then trying to use something like
    /<a*href=['\"][^'\"]*['\"]/i/
     
    3dom, Dec 29, 2007 IP
  8. pratham

    pratham Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    use /<a[^>]*=['"][^'"]*['"]/i if you want to match only a tags href. (Note: the escape char \ is for the php string)
     
    pratham, Dec 29, 2007 IP
  9. 3dom

    3dom Peon

    Messages:
    304
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Any ideas how does it looks like in working form? Because

    $_response_body = eregi_replace('/<a[^>]*=[\'"][^\'"]*[\'"]/i','<a href="#"',$_response_body);

    does nothing.
     
    3dom, Jan 10, 2008 IP
  10. rkquest

    rkquest Well-Known Member

    Messages:
    828
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    140
    #10
    Try this regexp:
    $_response_body = preg_replace('/<a(.*)href=\"(.*?)\"(.*)>/', '<a$1href="#"$3>', $_response_body)
    PHP:
    I tested it.. it works. :)
     
    rkquest, Jan 11, 2008 IP
  11. 3dom

    3dom Peon

    Messages:
    304
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Not with Reddit's front page - only with like 50% of links in there. Investigating.
    =(
     
    3dom, Jan 15, 2008 IP
  12. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Problem with that one is that it will mess you up if you come across something like this:

    <a name='whatever'>blahblahblah <a href='click here'>

    All the necessary pieces of the puzzle have been posted here, but strangely never together at once.

    What you want is closer to this:

    $new = preg_replace('/(<a[^>]+href=")[^"]+/i', '\1#', $old);
    PHP:
     
    SmallPotatoes, Jan 15, 2008 IP