Php 检测HTML源代码文件中的post值

Php 检测HTML源代码文件中的post值,php,html,post,detect,file-get-contents,Php,Html,Post,Detect,File Get Contents,我正在尝试创建一个通过PHP自动检测HTML文件中所有POST值的东西。例如,当您查看包含表单的网站源时,您将看到如下内容: <form id="someForm" action="register.php" method="post"> Name: <input type="text" name="username"><br> <!-- a post value "username" --> Email: <

我正在尝试创建一个通过PHP自动检测HTML文件中所有POST值的东西。例如,当您查看包含表单的网站源时,您将看到如下内容:

<form id="someForm" action="register.php" method="post">

    Name:
    <input type="text" name="username"><br> <!-- a post value "username" -->
    Email:
    <input type="text" name="email"><br> <!-- a post value "email" -->
    Password:
    <input type="password" name="password"><br> <!-- a post value "password" -->

    <input type="submit" value="Submit"> <!-- submit button -->

</form>

姓名:

电邮:
密码:
因此,我想编写一个PHP脚本,自动检测HTML文件中的所有POST值,并将它们排序到一个数组中

我想我会用这样的东西开始:

<?

  $file = file_get_contents("http://example.example/register.html");


?>

我需要在
$file
变量中搜索所有出现的“name=”,然后获取引号之间的单词。然后,将引号之间的文本排序为数组


我想自动检测HTML中提供的POST信息,并使用PHP脚本将这些值发布到该表单


我不知道我将使用哪个函数,或者如何使用,所以我在这里问。非常感谢示例代码。谢谢。

您可以将HTML加载到中并使用它进行查询,以查找特定的名称属性

例如:

$file = file_get_contents('url');

$dom = new DOMDocument;
$dom->loadHTML($file);

$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//@name'); //Find /all/ name attributes everywhere.
foreach ($nodes as $node) {
   echo "$node->value\n";
}

请记住,正如评论者所暗示的,输入以外的元素上可以有name属性,也可以有形式上的输入,而不是post方法。相应地修改XPath查询(“//input[@name]”查找所有具有名称属性的输入,例如,然后使用
$node->getAttribute('name')
而不是
$node->value

您可以将HTML加载到中并使用它进行查询,以专门查找名称属性

例如:

$file = file_get_contents('url');

$dom = new DOMDocument;
$dom->loadHTML($file);

$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//@name'); //Find /all/ name attributes everywhere.
foreach ($nodes as $node) {
   echo "$node->value\n";
}

请记住,正如评论员所暗示的,元素上可以有输入以外的名称属性,表单中也可以有非post方法的输入。相应地修改XPath查询(“//input[@name]”查找所有具有名称属性的输入,然后使用
$node->getAttribute('name'))
而不是
$node->value

您只是想自动化表单吗?您也可以这样做:

foreach($_POST as $key => $value)
{
    echo $key . ": " . $value . "<br />";
}
foreach($\u发布为$key=>$value)
{
echo$key.:“$value.”
; }
您只是想自动化表单吗?您也可以这样做:

foreach($_POST as $key => $value)
{
    echo $key . ": " . $value . "<br />";
}
foreach($\u发布为$key=>$value)
{
echo$key.:“$value.”
; }
你不能这样做吗

<input type="hidden" name="username_post" value="<?php if (isset($_POST['username'])) { echo $_POST['username']; } ?>" />
你能做什么

<input type="hidden" name="username_post" value="<?php if (isset($_POST['username'])) { echo $_POST['username']; } ?>" />

我想自动检测HTML中提供的POST信息,并有一个PHP脚本将这些值发布到该表单。我想自动检测HTML中提供的POST信息,并有一个PHP脚本将这些值发布到该表单。XPath查询类似于
//form[@method=“post”]/*[@name]
请给我举个例子好吗?XPath查询看起来像
//form[@method=“post”]/*[@name]
请给我举个例子好吗?当他说“post value”时,他的意思是“表单输入”,当他说“post value”时,他的意思是“表单输入”