gpt4 book ai didi

PHP Regex,匹配两个特定单词/标签之间的任何条件

转载 作者:行者123 更新时间:2023-12-04 04:48:01 37 4
gpt4 key购买 nike

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

我试图从包含多个表的网页中提取一些信息,只有一些表包含唯一的 url(假设“非常/唯一.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>

所以我想要的是提取所有包含“very/unique.key”关键字的表格内容。这是我尝试过的模式:
$pattern = "#<table[^>]+>((?!\<table)(?=very\/unique\.key).*)<\/table>#i";
这对我没有任何返回......
$pattern = "#<table[^>]+>((?!<table).*)<\/table>#i";
这将返回表 1 的打开标签中的所有内容 <table...>直到最后一个表的关闭标签 </table>即使使用 (?!<table)条件...

感谢任何愿意帮助我的人,谢谢。

-- 编辑 ——

这是我发现使用 DOM 循环遍历每个表的解决方案

-- 我的解决方案 ——
    $index;//indexes of all the table(s) that contains the keyword
$cd = 0;//counter

$DOM = new DOMDocument();
$DOM->loadHTMLFile("http://uni.corp/sub/sub/target.php?key=123");
$xpath = new DomXPath($DOM);
$tables = $DOM->getElementsByTagName("table");
for ($n = 0; $n < $tables->length; $n++) {
$rows = $tables->item($n)->getElementsByTagName("tr");
for ($i = 0; $i < $rows->length; $i++) {
$cols = $rows->item($i)->getElementsbyTagName("td");
for ($j = 0; $j < $cols->length; $j++) {


$td = $cols->item($j); // grab the td element
$img = $xpath->query('./img',$td)->item(0); // grab the first direct img child element


if(isset($img) ){
$image = $img->getAttribute('src'); // grab the source of the image
echo $image;
if($image == "very/unique.key"){
echo $cols->item($j)->nodeValue, "\t";
$index[$cd] = $n;
if($n > $cd){
$cd++;
}


echo $cd . " " . $n;//for troubleshooting
}


}

}
echo "<br/>";
}
}

//loop that echo out only the table(s) that I want which contains the keyword
$loop = sizeof($index);
for ($n = 0; $n < $loop; $n++) {
$temp = $index[$n];
$rows = $tables->item($temp)->getElementsbyTagName("tr");
for ($i = 0; $i < $rows->length; $i++) {
$cols = $rows->item($i)->getElementsbyTagName("td");
for ($j = 0; $j < $cols->length; $j++) {
echo $cols->item($j)->nodeValue, "\t";
//proccess the extracted table content here
}
//echo "<br/>";
}
}

但就我个人而言,我仍然对 Regex 部分感到好奇,希望任何人都能找到这个问题的 regex 模式的解决方案。无论如何,感谢所有在这方面帮助/建议我的人(尤其是 AbsoluteƵERØ)。

最佳答案

这适用于 PHP5。我们解析表和使用 preg_match()检查 key 。您想要使用这样的方法的原因是因为 HTMLXML 不同,不必按语法正确编写.因此,您实际上可能没有正确的结束标签。此外,您可能有嵌套表,这些表会为您提供多个结果,试图将开始和结束标记与 REGEX 匹配。这样我们只检查 key 本身,而不是正在解析的文档的良好形式。

<?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.
)

关于PHP Regex,匹配两个特定单词/标签之间的任何条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17888100/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com