>我有一个html代码:
<table id="table1" border="0" cellspacing="0" cellpadding="3" width="1" align="center">
<tr>
<td>
<img src="http://vnexpress.net/Files/Subject/3b/bd/ac/f9/cuongbibat.jpg" width="330" height="441" border="1" alt="Cường">
</td>
</tr>
<tr>
<td class="Image">Everything
</td>
</tr>
</table>
<table id="table2" border="0" cellspacing="0" cellpadding="3" width="1" align="center">
<tr>
<td>
Someone
</td>
</tr>
<tr>
<td class="Image">Everything
</td>
</tr>
</table>
我有 2 个表,我想删除所有标签:表、tr、td 如果表有 img 标签(表 1)。我需要得到这样的结果:
<img src="http://vnexpress.net/Files/Subject/3b/bd/ac/f9/cuongbibat.jpg" width="330" height="441" border="1" alt="Cường">
Everything
<table id="table2" border="0" cellspacing="0" cellpadding="3" width="1" align="center">
<tr>
<td>
Someone
</td>
</tr>
<tr>
<td class="text">Everything
</td>
</tr>
</table>
请帮助我。谢谢。
HTML净化器可用于从文档中去除所有标签或一组特定的标签。它是PHP中基本上任何HTML标签剥离的首选解决方案 - 永远不要为此使用正则表达式,否则太阳会燃烧殆尽,我们都会在令人窒息的黑暗中冻死。
尝试类似操作:
$config->set('HTML.Allowed', 'img');
$purifier = new HTMLPurifier($config);
$output = $filter->purify($YOUR_HTML);
您需要为每个不想被擦掉的标签添加一条$config->set('HTML.Allowed', 'TAGNAME');
线,但这是值得为白日星持续的生命温暖付出的代价。而且我猜也不会让您的网站受到 XSS 攻击和内容吞噬故障。
退房:http://simplehtmldom.sourceforge.net/
让我们在带有选择器的HTML页面上找到标签,就像jQuery一样,并在一行中提取HTML内容。
理论上,可以使用单个高度复杂的正则表达式来做到这一点。在单独的步骤中进行搜索和替换总是更容易:首先搜索外部容器,然后处理它包含的内容。
<?php
header("Content-type: text/plain");
$html = '<table id="table1" border="0" cellspacing="0" cellpadding="3" width="1" align="center">
<tr>
<td>
<img src="http://vnexpress.net/Files/Subject/3b/bd/ac/f9/cuongbibat.jpg" width="330" height="441" border="1" alt="Cường">
</td>
</tr>
<tr>
<td class="Image">Everything
</td>
</tr>
</table>
<table id="table2" border="0" cellspacing="0" cellpadding="3" width="1" align="center">
<tr>
<td>
Someone
</td>
</tr>
<tr>
<td class="Image">Everything
</td>
</tr>
</table> ';
$html = preg_replace_callback('/<table'b[^>]*>.*?<'/table>/si', 'removeTableIfImg', $html);
function removeTableIfImg($matches) {
$table = $matches[0];
return preg_match('/<img'b[^>]*>/i', $table, $img)
? preg_replace('/<'/?(?:table|td|tr)'b[^>]*>'s*/i', '', $table)
: $table;
}
echo $html;
?>
第一种模式查找表。第二种模式(在回调中)检查是否存在图像标记。第三个删除表、td 和 tr 标记。
我需要这样的东西。这是我的解决方案: (<'/?tr.*?>)|(<'/?td.*?>)|(<'/?table.*?>)
此正则表达式将选择所有不贪婪的 tr TD 和表标签。
您可以在此处看到它的实际效果:
http://regexr.com/3fslh
正如突然所说,不要为此使用正则表达式,它会让你发疯。通常,搜索库所花费的时间与为此编写自己的小型解析器所花费的时间相同。我用不同的语言做了几次。你学到了很多东西,你经常可以重用代码:-)
由于您对属性不感兴趣,这应该很容易。 逐个字符循环条目站点 char。 看看这个java代码,它是我早期的,较小的清理HTML的方法之一:
public static String sanatize(String body, String[] whiteList, String tagSeperator, String seperate) {
StringBuilder out = new StringBuilder();
StringBuilder tag = new StringBuilder();
boolean quoteOpen = false;
boolean tagOpen = false;
for(int i=0;i<body.length();i++) {
char c = body.charAt(i);
if(i<body.length()-1 && c == '<' && !quoteOpen && body.charAt(i+1) != '!') {
tagOpen = true;
tag.append(c);
} else if(c == '>' && !quoteOpen && tagOpen) {
tag.append(c);
for (String tagName : whiteList) {
String stag = tag.toString().toLowerCase();
if (stag.startsWith("</"+tagName+" ") || stag.startsWith("</"+tagName+">") || stag.startsWith("<"+tagName+" ") || stag.startsWith("<"+tagName+">")) {
out.append(tag);
} else if (stag.startsWith("</") && tagSeperator != null) {
if (seperate.length()>2) {
if (seperate.contains("," + stag.replaceAll("[</]+(''w+)[''s>].*", "$1") + ",")) {
out.append(tagSeperator);
}
} else {
if (!out.toString().endsWith(tagSeperator)) {
out.append(tagSeperator);
}
}
}
}
tag = new StringBuilder();
tagOpen = false;
} else if (c == '"' && !quoteOpen) {
quoteOpen = true;
if (tagOpen)
tag.append(c);
else
out.append(c);
} else if (i>1 && c == '"' && quoteOpen && body.charAt(i-1) != '''' ) {
quoteOpen = false;
if (tagOpen)
tag.append(c);
else
out.append(c);
} else {
if (tagOpen)
tag.append(c);
else
out.append(c);
}
}
return out.toString();
}
您可以忽略分隔符并分开,我用它来清理标签并转换为csv