January 2, 201115 yr I have a pattern, a string I'd like to search through, and I want the matching section of the string to be stored in a variable. Pattern: [0-9]*(?=\.gif$) String: http://figmint.zzl.org/photos/3.gif I tested this in a regex tester and it highlighted the "3", which is what I want. I've never done this before though, so I don't know what function I should be using for this, or how to go about it. Also, I was trying to test this on my own with an if/else, and it always outputs "no match" for some reason: <?php if (preg_match("[0-9]*(?=\.gif$)", "http://figmint.zzl.org/photos/3.gif")) { echo "it matched"; } else { echo "no match"; } ?>
January 2, 201115 yr Pattern: [0-9]*(?=\.gif$) String: http://figmint.zzl.org/photos/3.gif I tested this in a regex tester and it highlighted the "3", which is what I want. I've never done this before though, so I don't know what function I should be using for this, or how to go about it. Hi there, it seems you haven't included your regular expression delimiters, this would be the reason the code functioned for you in a regex tester, however under a server environment you are getting the following: Warning: preg_match() [function.preg-match]: Unknown modifier '*' in C:\Desktop\noname1.php5 on line 3 no match Thus despite regex delimiters not being required (they are stripped when processed) adding them will resolve your issue. <?php if (preg_match("#[0-9]*(?=\.gif$)#i", "http://figmint.zzl.org/photos/3.gif")) { echo "it matched"; } else { echo "no match"; } ?> These delimiters are simply a starting "#" and a finishing "#i" on the expression. Hope that helps Edited January 2, 201115 yr by CSN-UK
Create an account or sign in to comment