Cara menggunakan php preg_replace capture group

It tests to make sure that a string doesn't start with a $ and contains only word characters.  Simple enough, right?  Well, I was just playing around with named capture groups to see how PHP handles them.  It seems that they're added as an associative array key to the matches array:

Show
    <?php
    $str = 'testing';
    $matches = array();
    echo preg_match('/^(?<!\$)(?P<word>\w+)$/', $str, $matches);
    echo "\n";
    print_r($matches);
    ?>
    

     

    Outputs:

    Array
    (
        [0] => testing
        [word] => testing
        [1] => testing
    )
    

     

    Now, how would you reference that named capture in preg_replace() without using the numbered reference?  I know that you can use it inside the regex for backreferencing with (?P=word).  >_>  Anyone want to throw some advice out there? xD

    Link to comment
    Share on other sites

    More sharing options...

    Cara menggunakan php preg_replace capture group

    effigy

    Posted August 29, 2008

    effigy

    • Cara menggunakan php preg_replace capture group

    • Staff Alumni
    • Cara menggunakan php preg_replace capture group
      • 3.6k
    • Location: IL

      • Share

    Posted August 29, 2008

    I cannot find any information on this. My assumption is that it doesn't exist because there isn't a need for it. A data structure is being built when matching, so it's nice to be able to customize the key, whereas replacing is solely done by the engine.

     

    In the event that your replaces get nasty, the best approach is to use the /x modifier:

    /

    ### 1. Name

    (...)

    ### 2. Address

    (...)

    ### 3. And so forth...

    (...)

    /x

    Link to comment
    Share on other sites

    More sharing options...

    Cara menggunakan php preg_replace capture group

    DarkWater

    Posted August 29, 2008

    DarkWater

    • Cara menggunakan php preg_replace capture group

    • Members
      • 6.2k
    • Location: New York

    • Author

      • Share

    Posted August 29, 2008

    Yeah, I've used free spacing before for comments and stuff.  Thanks though.  Although, I think I read somewhere that in 5.2.4 you can use:

    \k<name>, \k'name', \k{name} or \g{name}

     

    To reference your named capture groups.  Not sure if it would work in preg_replace() though.  I'll need to go test it later. >_< 

     

    Found it on this page

    Link to comment
    Share on other sites

    More sharing options...

    Cara menggunakan php preg_replace capture group

    DarkWater

    Posted August 29, 2008

    DarkWater

    • Cara menggunakan php preg_replace capture group

    • Members
      • 6.2k
    • Location: New York

    • Author

      • Share

    Posted August 29, 2008

    Okay, apparently those don't work in preg_replace() either, but I do see where you're coming from, effigy.  Guess that won't work, lol.  But it IS handy to have in preg_match().

     

    Btw, if someone does know how to do this, please say so.  And to avoid any confusion, I'll say before hand that I changed the regex to:

    /\b(?<!\$)(?P<word>\w+)\b/

     

    Lets me get any words that don't have a $ at the start from a string.

    Just to see how preg_match_all() handled named groups.  Same as preg_match_all() always handles things; just a multi-dimensional array with the capture group's name included as one of the keys, which is pretty good to know.

    Link to comment
    Share on other sites

    More sharing options...

    Cara menggunakan php preg_replace capture group

    DarkWater

    Posted August 29, 2008

    DarkWater

    • Cara menggunakan php preg_replace capture group

    • Members
      • 6.2k
    • Location: New York

    • Author

      • Share

    Posted August 29, 2008

    Alright, I did a little bit more searching and still came up with nothing.  I still think it would be cool with replacing too, but at least PHP handles it nicely with the assoc. array.

    Link to comment
    Share on other sites

    More sharing options...

    Cara menggunakan php preg_replace capture group

    nrg_alpha

    Posted August 29, 2008

    nrg_alpha

    • Cara menggunakan php preg_replace capture group

    • Staff Alumni
    • Cara menggunakan php preg_replace capture group
      • 2.4k
    • Location: Canada's Capital

      • Share

    Posted August 29, 2008

    Well, I looked it up in the mastering regular expressions book.. it seems that in every instance of named captures, it always involves preg_match only. So perhaps it is only usable with this function? I could not find anything in the php.net manual (unless I'm just searching for the incorrect terms).