Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP提取HTML输入标记的值_Php_Html_Extract - Fatal编程技术网

如何使用PHP提取HTML输入标记的值

如何使用PHP提取HTML输入标记的值,php,html,extract,Php,Html,Extract,我知道正则表达式在这里并不流行,使用php脚本在HTML表单中提取输入标记值的最佳方法是什么 例如: 一些分区/表格等 一些分区/表格等 谢谢将表单发布到php页面。您所需的值将以$\u POST['id']为单位 你说regex不受欢迎是什么意思?我喜欢正则表达式 不管怎样,你想要的是: $contents = file_get_contents('/path/to/file.html'); preg_match('/value="(\w+)"/',$contents,$result);

我知道正则表达式在这里并不流行,使用php脚本在HTML表单中提取输入标记值的最佳方法是什么

例如:

一些分区/表格等


一些分区/表格等


谢谢

将表单发布到php页面。您所需的值将以$\u POST['id']为单位

你说regex不受欢迎是什么意思?我喜欢正则表达式

不管怎样,你想要的是:

$contents = file_get_contents('/path/to/file.html');
preg_match('/value="(\w+)"/',$contents,$result);

如果您想从一些HTML字符串中提取一些数据,最好的解决方案通常是使用可以将HTML加载到DOM树的类

然后,您可以使用任何与DOM相关的方法来提取数据,例如查询


在这里,您可以使用如下内容:

$html = <<<HTML
    <form action="blabla.php" method=post>

    <input type="text" name="campaign">  
    <input type="text" name="id" value="this-is-what-i-am-trying-to-extract">

    </form>
HTML;


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

$xpath = new DOMXPath($dom);

$tags = $xpath->query('//input[@name="id"]');
foreach ($tags as $tag) {
    var_dump(trim($tag->getAttribute('value')));
}
$html=newDOMDocument();
$html->loadHTML($html)
');
$els=$html->getelementsbytagname('input');
foreach($els作为$inp)
{
$name=$inp->getAttribute('name');
如果($name='id'){
$what\u you\u尝试提取=$inp->getAttribute('value');
打破
}
}
echo$您试图提取的内容;
//生产:这是我试图提取的东西

您是否有机会重新表述一下?发布时是否希望在php脚本中获取输入控件的值?如果是这样的话,这是非常基本的。也不确定在这种情况下关于regex的评论是什么?这是在生成页面时(使用php)还是在表单提交后?@spinon我认为他指的是[正确的]“你不能用regex解析HTML”的回复,但我认为他还没有意识到这不是他想做的,即使你可以。对不起,我应该解释清楚的。我有一个字符串变量,里面有一堆HTML。我需要从HTML字符串中提取输入标记的值,如果有帮助的话,输入标记的名称总是相同的(id)。每个人都喜欢它们。。。只是不用于HTML解析。请参阅,了解有关这方面的一些上下文!哈哈,我同意,正则表达式对于解析HTML是可怕的。然而,对于眼前的问题,正则表达式是一个完全有效的解决方案,尽管肯定不是唯一的解决方案?如果属性不是准确写入的,而是
value=“foo”
,该怎么办。如果HTML包含这个注释呢?所有这些都可以在正则表达式中解决。解决方案变得越来越不优雅。我并不主张将正则表达式作为解析像HTML这样格式松散的东西的解决方案。这个问题要求在PHP中为一个非常具体的封闭式情况使用正则表达式。同意,您的正则表达式确实有效,并且它符合这个问题,但最好的办法是建议询问者不要为此使用正则表达式,而是提出一个包含更好实践的解决方案。这将触发严格的标准:非静态方法DOMDocument::loadHTML()不应被称为staticallyYeah,我想。。。但它是有效的。不妨使用首选样式。请尝试
echo$xPath->evaluate('string(//input[@name=“id”]/@value)')
$html = <<<HTML
    <form action="blabla.php" method=post>

    <input type="text" name="campaign">  
    <input type="text" name="id" value="this-is-what-i-am-trying-to-extract">

    </form>
HTML;


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

$xpath = new DOMXPath($dom);

$tags = $xpath->query('//input[@name="id"]');
foreach ($tags as $tag) {
    var_dump(trim($tag->getAttribute('value')));
}
string 'this-is-what-i-am-trying-to-extract' (length=35)
$html=new DOMDocument();
$html->loadHTML('<form action="blabla.php" method=post>
    <input type="text" name="campaign">  
    <input type="text" name="id" value="this-is-what-i-am-trying-to-extract">
    </form>');

$els=$html->getelementsbytagname('input');

foreach($els as $inp)
  {
  $name=$inp->getAttribute('name');
  if($name=='id'){
    $what_you_are_trying_to_extract=$inp->getAttribute('value');
    break;
    }
  }

echo $what_you_are_trying_to_extract;
//produces: this-is-what-i-am-trying-to-extract