need some help with preg_replace and backreferences

Discussion in 'PHP' started by marcnyc, Jan 9, 2009.

  1. #1
    Hello, I am struggling with this.
    I am trying to grab something that is between two words and replace the entire string all these words come from with just the one thing that I matched.
    I know I am doing something wrong but I don't know enough about RegEx to figure it out.
    I have this:
    $string = 'words and words';
    // $desired_output = 'and';
    $desired_output = preg_replace('/words(.+)words/','$1',$string);

    can I get a nudge in the right direction please?
    Thanks
     
    marcnyc, Jan 9, 2009 IP
  2. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #2
    Do you want to print the "and" word? That`s what the code you gave us does!
     
    Fracisc, Jan 10, 2009 IP
  3. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Could you give us some more examples.
     
    harrisunderwork, Jan 10, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Why are you using preg_replace... preg_match would make a lot more sense...
     
    Danltn, Jan 10, 2009 IP
  5. marcnyc

    marcnyc Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I basically want to set two delimiting words and grab ONLY what is between those words and throw out everything else there is before and after what I grabbed, you know what I mean?
     
    marcnyc, Jan 10, 2009 IP
  6. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #6
    <?php
    
    $string = 'words and words';
    
    preg_match('/words(?:\s+)?(?P<output>.*)(?:\s+)?words/', $string, $matches);
    list(, $output) = array_map('trim', $matches);
    
    echo $output;
    PHP:
     
    Danltn, Jan 10, 2009 IP
  7. marcnyc

    marcnyc Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I figured out why it wasn't working.
    I was doing (.+)
    but instead it had to be (.+?)

    Thanks for all your help!

    You are right. I did that.
     
    marcnyc, Jan 10, 2009 IP
  8. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #8
    .+? makes the quantifier non-greedy (although more accurately, opposite of current behaviour as it can be toggled with a flag - but who cares about that).

    My solution worked fine, perhaps another part of your code wasn't ideal?

    Dan.
     
    Danltn, Jan 10, 2009 IP
  9. marcnyc

    marcnyc Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I wasn't implying that your solution wasn't working, I just found that with the question mark my code worked and only after that I read your post... I didn't try your code because I was following the "if it ain't broken don't fix it" philosophy ;-)
     
    marcnyc, Jan 10, 2009 IP
  10. marcnyc

    marcnyc Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I wasn't implying that your solution wasn't working, I just found that with the question mark my code worked and only after that I read your post... I didn't try your code because I was following the "if it ain't broken don't fix it" philosophy ;-)
     
    marcnyc, Jan 10, 2009 IP