php查找类似于name =“username”name =“password”的字符串模式

时间:2014-01-22 09:51:01

标签: php string preg-match

我正在尝试编写动态表单,其中php可以从给定的表单输入名称创建sql查询,用于exaple

<form name="f" method="post" action="#">
 <input type=text name="username">
 <input type=text name="email">
 <input type=text name="phone">
</form>

我想提取模式名称=“值”并从中提取“值”..我知道我可以使用 preg_match为此。但我不确定我怎么能这样做,请帮助我。 `

<?php
$subject = "<html><body><form name="f" method="post" action="#">
 <input type=text name="username">
 <input type=text name="email">
 <input type=text name="phone">
</form></html></body>";

$pattern = '/^def/'; // what patter should i use to extract only input type names
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>

提前谢谢

1 个答案:

答案 0 :(得分:0)

preg_match_all('/name="([^"]+)"/mi', $subject, $matches);

var_dump($matches[1]);

仅供参考:从html获取输入并不是一个好主意:)