1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.
  2. Better Analytics for WordPress Get It Free

preg_replace syntax

Discussion in 'PHP' started by THT, Aug 3, 2005.

  1. #1
    Im rubbish at regular expressions...


    heres what i want to do...

    replace:
    <A HREF="/search?q=keyword">keyword</A>

    with

    keyword


    I tried

    $var= preg_replace("/<A HREF=\"\/search?q=(.*)\">(.*)<\/A>/","$1",$var);

    But didnt seem to work... whats wrong...
     
    THT, Aug 3, 2005 IP
  2. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It could be to do with greediness - by default the regexp will try and match the longest string possible.
    You can switch this off by adding the U modifier, so
    $var= preg_replace("/<A HREF=\"\/search?q=(.*)\">(.*)<\/A>/U","$1",$var);​
    might work or else try matching just letters and numbers
    $var= preg_replace("/<A HREF=\"\/search?q=([a-zA-Z0-9]+*)\">([a-zA-Z0-9]+*)<\/A>/","$1",$var);​

    I think one of them will probably do it

    John
     
    johnt, Aug 3, 2005 IP
  3. THT

    THT Peon

    Messages:
    686
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for your help... but neither worked.... :(
     
    THT, Aug 3, 2005 IP
  4. forkqueue

    forkqueue Guest

    Messages:
    401
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm more of a perl guy than a PHP guy, but I reckon this should work:

    $newvar = preg_replace("/<.+>/g","",$var);
     
    forkqueue, Aug 3, 2005 IP
  5. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Your question mark is not escaped and regex is looking for searcq=keyword.

    J.D.
     
    J.D., Aug 3, 2005 IP
  6. THT

    THT Peon

    Messages:
    686
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yeah just came back to post thats what it was!
     
    THT, Aug 4, 2005 IP