Little preg_replace help

Discussion in 'PHP' started by redhits, Nov 9, 2009.

  1. #1
    I am doing this in order to change al href= to an #


    $search='/href=".+?"/sm';$data = preg_replace($search, 'href="#" onclick="seopink();"', $data);

    now.. the problem is that it will also change things like
    <link rel="stylesheet" type="text/css" href="#"
    and it will mess lot's of things...

    ... so i would like to change $search in order for the script to only search a*href...
     
    redhits, Nov 9, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $search = '/<a ([^>]*)\bhref="[^"]+"([^>]*)>/';
    $data = preg_replace($search, '<a $1href="#" onclick="seopink();"$2>', $data);
    PHP:
     
    JAY6390, Nov 9, 2009 IP
  3. Deus Ex

    Deus Ex Peon

    Messages:
    531
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That is the cleanest code, JAY. replace will work best, just keep it clean without too much complexity
     
    Deus Ex, Nov 9, 2009 IP