I have a string:
Where mnbztxxn is the first variable I want to match. And Blue is the second variable. I've tried a regexp like this:
but have had no luck. :( I suck at regexp. Any help? Thanks in advanced. You guys rock!
$hello = preg_match_all("/set_this_img\(\'http\:\/\/this\.site\.com\/cp\/(*.)\/1\/2\.png\', \'(*.)\'\)\;/", $content, $goodbye);
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. :eye:
$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);
// 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;
// Using string functions
$path = substr($subject, 38, strpos($subject, '/', 38) - 38);
$colour = substr($subject, 58, strpos($subject, "'", 58) - 58);
Not very nice to look at, but it works. :)
if(false !== preg_match_all("~(?<=')[^,]+?(?=')~", "set_this_img('http://this.site.com/cp/mnbztxxn/1/2.png', 'Blue');", $aMatches))
$sColour = $aMatches;
$sString = implode(null, array_splice(explode('/', $aMatches), 4, 1));
Ah, sscanf ! I really must utilise this more, thanks for yet another heads up Salathe. :)
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. :cool:
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. :eye:
Or, some helpful person might just come along and post a copy-and-paste-able code snippet that "just works"!