ballhogjoni Posted March 30, 2011 Share Posted March 30, 2011 I want to search the html for all the input fields in a form. I have preg_match_all("/<input\s[^>]*name=(\"??)([^\" >]*)\s[^>]*value=(\"??)([^\" >]*)\\1[^>]*>/i", $html, $matches, PREG_SET_ORDER) which somewhat works, but if the value has a space in it, preg_match_all only returns the first word. For example: <input type="hidden" name="description" value="Hello World"> preg_match_all will just return "Hello" instead of "Hello World" I hope that makes since. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/232126-need-help-with-preg_match_all/ Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 This is the part of the regex that is capturing the pattern after the value=.... ([^\" >]*) Notice that is a character class start with the "^" so it's matching any characters that are "not" in the character class. See the problem? Quote Link to comment https://forums.phpfreaks.com/topic/232126-need-help-with-preg_match_all/#findComment-1194053 Share on other sites More sharing options...
ballhogjoni Posted March 30, 2011 Author Share Posted March 30, 2011 got it figured out <input(?:\s*type='hidden')?(?:\s*type='submit')?\s*name='(.*)'\s*value='(.*)'.*> Quote Link to comment https://forums.phpfreaks.com/topic/232126-need-help-with-preg_match_all/#findComment-1194056 Share on other sites More sharing options...
.josh Posted March 30, 2011 Share Posted March 30, 2011 okay well even if you wanna disregard what gizmola is saying, at the very least you should use (.*?) instead of (.*) What you have now is a greedy match and you will get unexpected results! It's probably "working" for you right now because you have something like this: <input type='hidden' name='something1' value='blah1'> <input type='hidden' name='something2 value='blah2'> where inputs have linebreaks in them and you aren't using s modifier in your pattern. But if you run up against content where there is no line break between it... <input type='hidden' name='something1' value='blah1'><input type='hidden' name='something2 value='blah2'> run it on a string like that and see what happens! Quote Link to comment https://forums.phpfreaks.com/topic/232126-need-help-with-preg_match_all/#findComment-1194450 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.