Php 使用正则表达式从HTML表中查找结果
我遇到了一些正则表达式问题 我有一个巨大的html文件,我需要从文件中提取一些文本(型号)Php 使用正则表达式从HTML表中查找结果,php,regex,html-parsing,Php,Regex,Html Parsing,我遇到了一些正则表达式问题 我有一个巨大的html文件,我需要从文件中提取一些文本(型号) <table>...... <td colspan="2" align="center" class="thumimages"><b>SK10014</b></td></tr> ....... <table>/..... <td colspan="2" align="center" class="thumimag
<table>......
<td colspan="2" align="center" class="thumimages"><b>SK10014</b></td></tr>
.......
<table>/.....
<td colspan="2" align="center" class="thumimages"><b>SK1998</b></td></tr>
.... so on
。。。。。。
SK10014
.......
/.....
SK1998
.... 等等
这是一个巨大的网页,所有网页都内置在表格中,并且没有分区
“thumimages”类几乎在所有td中重复,因此无法区分页面中需要的内容
大约有10000个型号,我需要提取它们
有没有办法用regrex做这件事。。。像
"/<td colspan="2" align="center" class="thumimages"><b>{[1-9]}</b></td></tr>/"
“/{[1-9]}/”
并返回所有匹配结果的数组。注意:我已经尝试过HTML解析,但是文档包含很多HTML验证错误
任何帮助都将不胜感激……/()([a-z0-9]+)()/i
/(<td colspan="2" align="center" class="thumimages"><b>)([a-z0-9]+)(</b></td></tr>)/i
这是有效的。/()([a-z0-9]+)()/i
这很有效。您可以使用php类
您可以使用php类
说明
这将使所有td
字段与class=“thumimages”
匹配,并检索内部b
标记的内容。内部文本需要有一些值,任何前导或尾随空格都将被删除
]*\s\bclass=([“'])thumimages\1)[^>]*>\s*(?!Description
这将使所有td
字段与class=“thumimages”
匹配,并检索内部b
标记的内容。内部文本需要有一些值,并且任何前导或尾随空格都将被删除
]*\s\bclass=([“'])thumimages\1)[^>]*>\s*(?!带有DOMDocument的方法:
$pattern = <<<'LOD'
~
<td (?>[^>c]++|\bc(?!lass\b))+ # begining of td tag until the word "class"
class \s*+ = \s*+ # "class=" with variable spaces around the "="
(["']?+) thumimages\b \1 # "thumimages" between quotes or not
(?>[^>]++|(?<!b)>)+> # all characters until the ">" from "<b>"
\s*+ \K # any spaces and pattern reset
[^<\s]++ # all chars that are not a "<" or a space
~xi
LOD;
preg_match_all($pattern, $html, $matches);
echo '<pre>' . print_r($matches[0], true);
/$html代表您的html内容
$doc=新的DOMDocument();
@$doc->loadHTML($html);
$td_nodes=$doc->getElementsByTagName('td');
foreach($td\u节点作为$td\u节点){
if($td_node->getAttribute('class')=='thumimages')
echo$td_node->firstChild->textContent.“
”;
}
使用正则表达式的方法:
$pattern=“from”
\s*+\K#任何空格和图案重置
[^使用DOMDocument的方法:
$pattern = <<<'LOD'
~
<td (?>[^>c]++|\bc(?!lass\b))+ # begining of td tag until the word "class"
class \s*+ = \s*+ # "class=" with variable spaces around the "="
(["']?+) thumimages\b \1 # "thumimages" between quotes or not
(?>[^>]++|(?<!b)>)+> # all characters until the ">" from "<b>"
\s*+ \K # any spaces and pattern reset
[^<\s]++ # all chars that are not a "<" or a space
~xi
LOD;
preg_match_all($pattern, $html, $matches);
echo '<pre>' . print_r($matches[0], true);
/$html代表您的html内容
$doc=新的DOMDocument();
@$doc->loadHTML($html);
$td_nodes=$doc->getElementsByTagName('td');
foreach($td\u节点作为$td\u节点){
if($td_node->getAttribute('class')=='thumimages')
echo$td_node->firstChild->textContent.“
”;
}
使用正则表达式的方法:
$pattern=“from”
\s*+\K#任何空格和图案重置
[^不要使用正则表达式来解析HTML。你无法用正则表达式可靠地解析HTML,你将面临悲伤和挫折。一旦HTML与你的预期不同,你的代码将被破坏。有关如何使用已经编写、测试和删除的PHP模块正确解析HTML的示例,请参阅ugged。不要使用正则表达式来解析HTML。你无法用正则表达式可靠地解析HTML,你将面临悲伤和挫折。一旦HTML偏离你的预期,你的代码将被破坏。有关如何使用已编写、测试和测试的PHP模块正确解析HTML的示例,请参阅已调试。我得到了一个空数组..数组([0]=>Array()[1]=>Array()[2]=>Array()[3]=>Array())..我使用了preg_match_all('|()([a-z0-9]+)()| I',$content,$matchesarray);我想你需要用\某些html字符转义,比如/“,也许=我得到了一个空数组..数组([0]=>Array()[1]=>Array()[2]=>Array()[3]=>Array())…我使用了preg_match_all('|()([a-z0-9]+)()| I',$content,$matchesarray);我认为需要使用\某些html字符转义,如/“and Obay=尝试过,但文档包含了到多个html验证错误。尝试过,但文档包含了到多个html验证错误。我同意html解析可能是最好的解决方案,但是请求者在这里的另一个答案上留下了评论,说html源代码格式不好,正在删除验证错误。我同意HTML解析可能是最好的解决方案,但是请求者确实在这里的另一个答案上留下了评论,说HTML源代码格式不好,正在删除验证错误。
<?php
$sourcestring="your source string";
preg_match_all('/<td\b(?=\s)(?=[^>]*\s\bclass=(["'])thumimages\1)[^>]*><b>\s*(?!<)([^<\s]+)\s*<\/b><\/td>/imsx',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
$matches Array:
(
[0] => Array
(
[0] => <td colspan="2" align="center" class="thumimages"><b>SK10014</b></td>
[1] => <td colspan="2" align="center" class="thumimages"><b> SK1998 </b></td>
)
[1] => Array
(
[0] => "
[1] => "
)
[2] => Array
(
[0] => SK10014
[1] => SK1998
)
)
// $html stands for your html content
$doc = new DOMDocument();
@$doc->loadHTML($html);
$td_nodes = $doc->getElementsByTagName('td');
foreach($td_nodes as $td_node){
if ($td_node->getAttribute('class')=='thumimages')
echo $td_node->firstChild->textContent.'<br/>';
}
$pattern = <<<'LOD'
~
<td (?>[^>c]++|\bc(?!lass\b))+ # begining of td tag until the word "class"
class \s*+ = \s*+ # "class=" with variable spaces around the "="
(["']?+) thumimages\b \1 # "thumimages" between quotes or not
(?>[^>]++|(?<!b)>)+> # all characters until the ">" from "<b>"
\s*+ \K # any spaces and pattern reset
[^<\s]++ # all chars that are not a "<" or a space
~xi
LOD;
preg_match_all($pattern, $html, $matches);
echo '<pre>' . print_r($matches[0], true);