问题
Sorry, i spent like 2 hours trying to preg_match this form
<form class="form-search" method="post" action="/index.php">
<div class="form-group">
<input id="address_box" type="text" class="form-control" name="x" value="" onfocus="this.select()" />
</div>
<span class="btn btn-s btn-caps"><input type="submit" value="start" /></span>
</form>
To:
Preg_match:
START = <form
WHERE action CONTAIN /index.php
EX: action="/index.php" or action="http://whatever.com/index.php"
FIND name="[A-Za-z]{1}"
END = </form>
Then Output the [A-Za-z]{1} Match (Should get x)
How can i do it correctly please?
Thanks.
回答1:
$pat = /\/index.php\">.*?form-control\".*(?<=name=\")([A-Za-z]{1})(?=\")/s
$sub = '<form class="form-search" method="post" action="/index.php">
<div class="form-group">
<input id="address_box" type="text" class="form-control" name="x" value="" onfocus="this.select()" />
</div>
<span class="btn btn-s btn-caps"><input type="submit" value="start" /></span>
</form>';
preg_match($pat,$sub,$match);
to echo mateched use echo $match[1];
回答2:
Ok, this regex should do the job:
$regex = '/<form.*(?<=action=\")?\/index.php\">.*(?<=name=\")([A-Za-z]{1})(?=\").*?<\/form>/s';
来源:https://stackoverflow.com/questions/29052172/preg-match-name-of-input-field-inside-form