Jump to content

Need help with preg_match_all


ballhogjoni

Recommended Posts

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

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.