Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.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正则表达式,用条件匹配两个特定单词/标记之间的任何内容_Php_Regex_Html Parsing - Fatal编程技术网

PHP正则表达式,用条件匹配两个特定单词/标记之间的任何内容

PHP正则表达式,用条件匹配两个特定单词/标记之间的任何内容,php,regex,html-parsing,Php,Regex,Html Parsing,我的正则表达式很差,这是我的情况 我试图从包含多个表的网页中提取一些信息,只有一些表包含唯一的url(比如“very/unique.key”),因此它看起来如下所示: <table ....> (bunch of content) </table> <table ....> (bunch of content) </table> <table ....> (bunch of content + "very/uniq

我的正则表达式很差,这是我的情况

我试图从包含多个表的网页中提取一些信息,只有一些表包含唯一的url(比如“very/unique.key”),因此它看起来如下所示:

<table ....>
   (bunch of content)
</table>

<table ....>
   (bunch of content)
</table>

<table ....>
   (bunch of content + "very/unique.key" keyword)
</table>

<table ....>
   (bunch of content)
</table>

<table ....>
   (bunch of content + "very/unique.key" keyword)
</table>

(一堆内容)
(一堆内容)
(大量内容+非常/唯一的.key关键字)
(一堆内容)
(大量内容+非常/唯一的.key关键字)
所以我想要的是提取包含“very/unique.key”关键字的所有表的内容。以下是我尝试过的模式:


$pattern=“#]+>(?!\(?!虽然我同意你帖子上的评论,但我会给出解决方案。如果你想用其他东西替换非常/unique.key,正确的正则表达式如下所示

#<table(.*)>((.*)very\/unique\.key(.*))<\/table>#imsU
#(.*)非常\/unique\.key(.*)#imsU
这里的关键是使用正确的修饰符使其与输入字符串一起工作。有关这些修饰符的更多信息,请参阅

下面是一个示例,我将very/unique.key替换为“foobar”


这段代码打印完全相同的字符串,但是两个“very/unique.key”被“foobar”替换,就像我们想要的那样

虽然这个解决方案可以工作,但它肯定不是最有效也不是最容易使用的。正如Mehdi在评论中所说,PHP有一个专门针对XML(因此是HTML)的扩展

这里有一个指向该扩展的文档的链接


使用它,您可以轻松地遍历每个表元素并找到具有唯一键的元素。

这在PHP5中起作用。我们解析表并使用
preg_match()
检查密钥。之所以要使用这样的方法,是因为
HTML
不必像
XML
那样以正确的语法编写。因此,您可能实际上没有正确的结束标记。此外,您可能有嵌套的表,这些表将为您提供多个与openin匹配的结果这样我们只检查密钥本身,而不是正在解析的文档的良好形式

<?php

$input = "<html>
<table id='1'>
<tr>
<td>This does not contain the key.</td>
</tr>
</table>
<table id='2'>
<tr>
<td>This does contain the unique.key!</td>
</tr>
</table>

<table id='3'>
<tr>
<td>This also contains the unique.key.</td>
</tr>
</table>

</html>";

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

$findings = array();

$tables = $html->getElementsByTagName('table');
foreach($tables as $table){

    $element = $table->nodeValue;

    if(preg_match('!unique\.key!',$element)){
        $findings[] = $element;
    }
}

print_r($findings);

?>

为什么首先要使用正则表达式?关于:不要使用正则表达式来解析HTML。你不能用正则表达式可靠地解析HTML,你将面临悲伤和挫折。一旦HTML偏离你的预期,你的代码将被破坏。有关如何使用PHP模块正确解析HTML的示例,请参阅已经编写、测试和调试过的。这似乎是问题所在。您真正的问题/问题是如何获取包含所述字符串的表元素。使用正则表达式只是另外一种解决方案,它并没有真正起作用。我试图解析/提取的网页是一个使用AJAX/php/JS生成的动态页面动态内容。因此网页中的大多数html元素没有id/class之类的唯一标识符。而且因为内容是动态的,所以我觉得使用DOM进行解析可能要困难得多,尽管我在正则表达式方面也很差。这是一个内联网网页,我用它来解析一定数量的信息,但这不应该超过1个月(我猜)。感谢您回复我,并感谢任何人能够在使用DOM或Regex或其他方面给我更多启发…感谢您的回复,但不幸的是,它没有给我任何回报:(,无论如何,再次感谢您愿意帮助我编辑我的答案,以提供更多细节和更完整的Regex:)对DOM也是新手,但两者都使用似乎不错。但我有两个问题,1)我需要具有唯一键的整个表内容,而不是只有键,2)我可以将类似“”的内容作为loadHTML()的输入吗?因为它会给我返回一个空数组,即使我尝试无条件地循环遍历每个元素。但是无论如何,感谢你为我指明了一个新的方向,这很有帮助,再次感谢。以前犯了一些愚蠢的错误,现在DOM部分工作得很好,只需要找出正则表达式部分,它不工作。啊,正则表达式,我讨厌重复gex…无论如何,再次感谢。@user2619841确切的键是什么?您是否正确转义了它?正如我前面提到的,它是一个类似于“sub/filename.ext”的url,不知道为什么正则表达式不起作用,并且没有返回任何内容给我,但是感谢您指导我使用DOM,我找到了一个解决方案来获取整个表的内容,请查看我的答案,谢谢。
<?php
$string = "
<table ....>
   (bunch of content)
</table>

<table ....>
   (bunch of content)
</table>

<table ....>
   bunch of content very/unique.key 
</table>

<table ....>
   (bunch of content)
</table>

<table ....>
   blabla very/unique.key
</table>
";

$pattern = '#<table(.*)>((.*)very\/unique\.key(.*))<\/table>#imsU';

echo preg_replace($pattern, '<table$1>$3foobar$4</table>', $string);
?>
<?php

$input = "<html>
<table id='1'>
<tr>
<td>This does not contain the key.</td>
</tr>
</table>
<table id='2'>
<tr>
<td>This does contain the unique.key!</td>
</tr>
</table>

<table id='3'>
<tr>
<td>This also contains the unique.key.</td>
</tr>
</table>

</html>";

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

$findings = array();

$tables = $html->getElementsByTagName('table');
foreach($tables as $table){

    $element = $table->nodeValue;

    if(preg_match('!unique\.key!',$element)){
        $findings[] = $element;
    }
}

print_r($findings);

?>
Array
(
    [0] => This does contain the unique.key!
    [1] => This also contains the unique.key.
)