preg_match表单内输入字段的名称

时间:2015-03-14 17:48:16

标签: php forms preg-match

抱歉,我花了2个小时尝试preg_match这个表格

<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:

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)

我怎么能正确地做到这一点?

感谢。

2 个答案:

答案 0 :(得分:0)

$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];

答案 1 :(得分:0)

好的,这个正则表达式应该可以胜任:

$regex = '/<form.*(?<=action=\")?\/index.php\">.*(?<=name=\")([A-Za-z]{1})(?=\").*?<\/form>/s';