preg_match name of input field inside form

杀马特。学长 韩版系。学妹 提交于 2019-12-11 02:40:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!