SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Pandora Tipem's Avatar
    Join Date
    Feb 2006
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match_all question

    Hi,

    I have a string:

    Code:
    set_this_img('http://this.site.com/cp/mnbztxxn/1/2.png', 'Blue');
    Where mnbztxxn is the first variable I want to match. And Blue is the second variable. I've tried a regexp like this:

    PHP Code:
    $hello preg_match_all("/set_this_img\(\'http\:\/\/this\.site\.com\/cp\/(*.)\/1\/2\.png\', \'(*.)\'\)\;/"$content$goodbye); 
    but have had no luck. I suck at regexp. Any help? Thanks in advanced. You guys rock!
    Tipem no fancy sig, yup
    Need Neopets Help? Try Neopets
    fun adoptable eggs

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    There are a heap of different ways to get at those variable strings. Here's a few which may or may not help! Any questions, just ask.

    PHP Code:
    $subject "set_this_img('http://this.site.com/cp/mnbztxxn/1/2.png', 'Blue');";

    // Using sscanf         
    sscanf($subject"set_this_img('http://this.site.com/cp/%[a-z]/1/2.png', '%[a-zA-Z]');"$path$colour);
    var_dump($path$colour);

    // Using PCRE
    preg_match('~^set_this_img\(\'http://this\.site\.com/cp/([a-z]+)/1/2\.png\', \'([a-z]+)\'\);$~iD'$subject$match);
    list(, 
    $path$colour) = $match;
    var_dump($path$colour);

    // Using string functions
    $path   substr($subject38strpos($subject'/'38) - 38);
    $colour substr($subject58strpos($subject"'"58) - 58);
    var_dump($path$colour); 
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Not very nice to look at, but it works.

    PHP Code:
    <?php
    if(false !== preg_match_all("~(?<=')[^,]+?(?=')~""set_this_img('http://this.site.com/cp/mnbztxxn/1/2.png', 'Blue');"$aMatches))
    {
        
    $sColour $aMatches[0][1];
        
    $sString implode(nullarray_splice(explode('/'$aMatches[0][0]), 41));
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ah, sscanf ! I really must utilise this more, thanks for yet another heads up Salathe.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    Pandora Tipem's Avatar
    Join Date
    Feb 2006
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys,

    Thanks for your help. I'm sorry for not being as explanatory:

    1. There will be multiple instances of set_this_img(...) and all instances must be matched. It's not just one string. Sorry for not making that clear :P

    2. set_this_img(...) will be located in the midst of other characters (like a, #, $, <, etc... basically any character), so I think the delimiter ~^ won't work, will it? Sorry, I don't know anything about regexp, but is that why your PCRE function doesn't work when I make it preg_match_all, Salathe?

    Any ideas of how to match all instances in the midst of any characters (basically $)?

    Thanks again for the help... this is great.
    Tipem no fancy sig, yup
    Need Neopets Help? Try Neopets
    fun adoptable eggs

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Only the tilde character (~) is a delimiter, notifying the start and end of the pattern itself and separating that from any pattern modifiers (i and D in this case). The caret (^) anchors the match to the start of the string.

    Since you are unfamiliar with regular expressions, it perhaps wouldn't be much use providing you with another one to cater for the changes mentioned above. It is no good having code that you can't understand easily. If you want to continue the regular expression route, try and soak in some good documentation on the subject (eg, here & here) and come up with what you think should (but might not) work. We can go from there.

    Or, some helpful person might just come along and post a copy-and-paste-able code snippet that "just works"!
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •