Php 如何使用preg_match()从表单中获取输入字段值

Php 如何使用preg_match()从表单中获取输入字段值,php,regex,curl,preg-match,Php,Regex,Curl,Preg Match,我不是这个领域的专家,所以请帮助我,如果我有无知的话,请原谅我。 我试图卷曲一个页面,想要得到隐藏的字段的值。我不熟悉regexp。我的代码如下: $page = curl_exec($ch); } curl_close($ch); function parse_form_fields($page, $username, $password){ preg_match("/<input id=\"signuptoken\" type=\"hidden\" value=

我不是这个领域的专家,所以请帮助我,如果我有无知的话,请原谅我。 我试图卷曲一个页面,想要得到隐藏的
字段的值。我不熟悉regexp。我的代码如下:

       $page = curl_exec($ch);
}
curl_close($ch);

function parse_form_fields($page, $username, $password){
    preg_match("/<input id=\"signuptoken\" type=\"hidden\" value=\"(.+?)\" name=\"signuptoken\"/", $page, $m);

    $captchatoken = $m[1];

    $parameters[] = "newaccounttoken=" . urlencode($captchatoken);
}
<input id="signuptoken" type="hidden" value="03AHJ_Vuv2ts6ev2LltAkZB91vjD6k-BsW3286bTC9QZYZLSHQUMNDQJFUaNmAQMAYb9FDhIkOFzAisafasfsTZuv_pl5KvkYNfsGUPcOAEX5YPlMaMOi7MZJq4ky0v_GyM60SmMgjPrtfZSJYE0hqw--GsfsafasmER0Sksr6OAvnLnBVAMsKcCi7uM" name="signuptoken">
$page=curl\u exec($ch);
}
卷曲关闭($ch);
函数解析表单字段($page、$username、$password){

preg_match(“/这应该可以帮助您找到值:

<?php
$input  = '<input id="signuptoken" type="hidden" value="03AHJ_Vuv2ts6ev2LltAkZB91vjD6k-BsW3286bTC9QZYZLSHQUMNDQJFUaNmAQMAYb9FDhIkOFzAisafasfsTZuv_pl5KvkYNfsGUPcOAEX5YPlMaMOi7MZJq4ky0v_GyM60SmMgjPrtfZSJYE0hqw--GsfsafasmER0Sksr6OAvnLnBVAMsKcCi7uM" name="signuptoken">';

$result = preg_match('/<input id="signuptoken" type="hidden" value="(.*?)"/', $input, $matches);
if(!$result){
    // Could not find input
} else {
    // Input value found
    echo 'Value: '.$matches[1];
}

这应该可以帮助您找到值:

<?php
$input  = '<input id="signuptoken" type="hidden" value="03AHJ_Vuv2ts6ev2LltAkZB91vjD6k-BsW3286bTC9QZYZLSHQUMNDQJFUaNmAQMAYb9FDhIkOFzAisafasfsTZuv_pl5KvkYNfsGUPcOAEX5YPlMaMOi7MZJq4ky0v_GyM60SmMgjPrtfZSJYE0hqw--GsfsafasmER0Sksr6OAvnLnBVAMsKcCi7uM" name="signuptoken">';

$result = preg_match('/<input id="signuptoken" type="hidden" value="(.*?)"/', $input, $matches);
if(!$result){
    // Could not find input
} else {
    // Input value found
    echo 'Value: '.$matches[1];
}
您最好使用。例如:

$html = '<input id="signuptoken" type="hidden" value="03AHJ_Vuv2ts6ev2LltAkZB91vjD6k-BsW3286bTC9QZYZLSHQUMNDQJFUaNmAQMAYb9FDhIkOFzAisafasfsTZuv_pl5KvkYNfsGUPcOAEX5YPlMaMOi7MZJq4ky0v_GyM60SmMgjPrtfZSJYE0hqw--GsfsafasmER0Sksr6OAvnLnBVAMsKcCi7uM" name="signuptoken">';
$dom = new DomDocument();
$dom->loadHTML($html);

$signuptoken = $dom->getElementById("signuptoken");
echo $signuptoken->getAttribute('value');
$html='';
$dom=新的DomDocument();
$dom->loadHTML($html);
$signuptoken=$dom->getElementById(“signuptoken”);
echo$signuptoken->getAttribute('value');
您最好使用。例如:

$html = '<input id="signuptoken" type="hidden" value="03AHJ_Vuv2ts6ev2LltAkZB91vjD6k-BsW3286bTC9QZYZLSHQUMNDQJFUaNmAQMAYb9FDhIkOFzAisafasfsTZuv_pl5KvkYNfsGUPcOAEX5YPlMaMOi7MZJq4ky0v_GyM60SmMgjPrtfZSJYE0hqw--GsfsafasmER0Sksr6OAvnLnBVAMsKcCi7uM" name="signuptoken">';
$dom = new DomDocument();
$dom->loadHTML($html);

$signuptoken = $dom->getElementById("signuptoken");
echo $signuptoken->getAttribute('value');
$html='';
$dom=新的DomDocument();
$dom->loadHTML($html);
$signuptoken=$dom->getElementById(“signuptoken”);
echo$signuptoken->getAttribute('value');

不要使用
值=\”(.+?)\“
,在一些格式错误的HTML中,它们可能会有很多问题。请使用一些更具限制性的东西,例如
值=\”([^\>]+?)\”
。区别在于
匹配的实体比
[^>]
多得多,后者总是在标记关闭或引号关闭时结束


您的问题可能是缺少多行匹配修饰符,请尝试
preg\u match('/不要使用
value=\“(.+?)\”
,在一些格式错误的HTML中,它们可能会有很多问题。请使用限制性更强的东西,如
value=\“([^\>]+?)\”
。不同之处在于
匹配的实体比
[^”>]
多得多,后者总是在标记关闭或报价关闭时结束


您的问题可能是缺少多行匹配修饰符,请尝试
preg_match('/谢谢Adam,它适用于变量(即$input),我想在我翻阅的网页中找到它($page=curl_exec($ch);@DeepakVerma您可以实际使用
[file\u get\u contents](http://php.net/manual/en/function.file-get-contents.php)
如果PHP安装设置正确(可能是这样),要加载URL,只需将示例中的
$input='…';
替换为
$input=file_get_contents(“[your URL]”);
即可,正则表达式的作用是什么,我尝试了上面的方法,但无效。感谢Adam,它对变量有效(即$input)我想在我翻阅过的网页($page=curl\u exec($ch);)@DeepakVerma您可以实际使用
[file\u get\u contents](http://php.net/manual/en/function.file-get-contents.php)
如果PHP安装设置正确,则加载URL(可能是这样)–只需将示例中的
$input='…';
替换为
$input=file\u get\u内容(“[您的url]”)
这应该行得通正则表达式的作用是什么,我尝试了上面的一个,但没有work@DeepakVerma很高兴知道答案。一定要接受答案,这样其他有相同问题的人就会知道什么解决方案有效。@DeepakVerma很高兴知道答案有效。一定要接受答案,这样其他有相同问题的人就会知道什么解决方案工作。