I am trying to match a string like the following:
<input type="text" value="cbyEOS56RK3lOxtiCrhmWSkDuNWwrFN4" name="iden">
This is my code:
$pattern = '~value="(.+?)" name="iden"~';
preg_match($pattern, $page, $match);
print_r($match);
As you can probably see, I am trying to match the value in this HTML input. By what I know of regular expressions, .*
will match as many characters as possible until it satisfies the next token (in this case "
).
I have the name="iden"
part in my regex because there are other HTML inputs on the page and I only want to match this one.
Problem is, I'm not getting any matches at all. $match
is an empty array. And I know that $page
has the right c开发者_Go百科ontent because I can see it when I echo it.
Help fixing my regex is appreciated, thanks.
Since you used the phrase "there are other inputs on the page", I assume you're trying to parse out this particular tag from a full HTML document. In that case, I recommend using a DOM parser rather than regular expressions (I'm not trying to be facetious with that link, there's just a lot of options so that seemed easiest). They are designed specifically for this purpose and will be a lot easier in the end.
If you want to try regex anyway, I would personally use ([^"]+)
instead of (.+?)
:
$pattern = '~value="([^"]+)" name="iden"~';
Though this still doesn't address whatever is causing your problem, as your regex should match on that line.
精彩评论