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.

preg_match_all and general characters

Discussion in 'PHP' started by FunkyFresh, May 20, 2011.

  1. #1
    what does preg_match_all("/<option\svalue\s='([^']+)'>([^>]+)<\/option>/", $txt, $rs);
    do?

    I understand it matches the pattern but what is all the extra back slashes, '\s', '([^', etc. mean.

    thanks for any help!
     
    FunkyFresh, May 20, 2011 IP
  2. Aotearoa

    Aotearoa Member

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    \s is any whitespace character. See the PCRE Escape sequences page in the PHP manual for a list http://nz.php.net/manual/en/regexp.reference.escape.php

    [^']+ says one or more characters that are not apostrophes. It's fairly standard for regular expression syntaxes and is probably documented in a page close to the one above

    HTH

    Bruce
     
    Aotearoa, May 20, 2011 IP
  3. FunkyFresh

    FunkyFresh Peon

    Messages:
    499
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    so assuming that $txt=<option value='423122' >Della 1</option>

    would this be the expected array:
    $rs[0]=<option value='423122' >Della 1</option>
    $rs[1]=value='423122' >Della 1</option>
    $rs[2]=='423122' >Della 1</option>
    $rs[3]=423122' >Della 1</option>

    I am not sure what the rest would be
     
    FunkyFresh, May 20, 2011 IP
  4. Aotearoa

    Aotearoa Member

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    Try this
    <?php
    $txt="<option value='423122' >Della 1</option>";
    $rs=array();
    $pat="/<option\s+value\s*=\s*'([^']+)'[^>]*>([^<]*)<\/option>/";
    print "preg_match\n";
    print preg_match( $pat, $txt, $rs ) . " ";
    print $txt . "\n";
    print_r( $rs );
    print "\n\n";
    print "preg_match_all\n";
    print preg_match_all( $pat, $txt, $rs ) . " ";
    print $txt . "\n";
    print_r( $rs );
    Code (markup):
    Output
    
    preg_match
    1 <option value='423122' >Della 1</option>
    Array
    (
        [0] => <option value='423122' >Della 1</option>
        [1] => 423122
        [2] => Della 1
    )
    
    
    preg_match_all
    1 <option value='423122' >Della 1</option>
    Array
    (
        [0] => Array
            (
                [0] => <option value='423122' >Della 1</option>
            )
    
        [1] => Array
            (
                [0] => 423122
            )
    
        [2] => Array
            (
                [0] => Della 1
            )
    
    )
    
    Code (markup):
    Neither of these are exactly your expected output. The preg_match output seems to be closer to it than the preg_match.

    HTH

    Bruce
     
    Aotearoa, May 20, 2011 IP
  5. FunkyFresh

    FunkyFresh Peon

    Messages:
    499
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I see you have changed the pattern alittle to make it work. thanks for your help!
     
    FunkyFresh, May 22, 2011 IP
  6. Aotearoa

    Aotearoa Member

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    Yes,

    Sorry I forgot to mention that I'd done that.

    I got carried away with finding out what the differences were between the output of preg_match_all and preg_match and by the time I wrote up my results it had slipped my mind that I'd had to debug the pattern at the start of the process.

    I hope it didn't take you too long to work out that I'd changed it.

    Bruce
     
    Aotearoa, May 22, 2011 IP