需要 preg_replace 方面的帮助

发布于 2024-11-14 11:47:07 字数 742 浏览 1 评论 0原文

$text = '<p width="50px;" style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p><table style="text-align:center"></table>';

$text_2 = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', $text);

输出(我在这里给出了html格式):

<p>
<strong>hello</strong>
</p>
<table></table>

我的问题是所有属性都必须删除,但不是属于表的属性。也就是说,我期待的输出与下面完全相同(HTML FORMAT):

<p>
<strong>hello</strong>
</p>
<table style="text-align:center"></table>

我需要在上面的正则表达式中修改什么才能实现它。

任何帮助将不胜感激......

提前致谢...

$text = '<p width="50px;" style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p><table style="text-align:center"></table>';

$text_2 = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', $text);

OUTPUT(i have given the html format here):

<p>
<strong>hello</strong>
</p>
<table></table>

My problem is all attributes must be removed but not the attributes belongs to table. That is i am expecting the out put exactly like below(HTML FORMAT):

<p>
<strong>hello</strong>
</p>
<table style="text-align:center"></table>

What should i need to modify in the above regular expression to achieve it..

Any help will be thankful and grateful....

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生再见 2024-11-21 11:47:07

如果您想避免使用正则表达式,因为您确实不应该使用正则表达式来处理 xml/html 结构,请尝试:

<?php
$text = '<p width="50px;" style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p><table style="text-align:center"></table>';

$dom = new DOMDocument;
$dom->formatOutput = true;
$dom->loadHtml($text);

$xpath = new DOMXpath($dom);
foreach ($xpath->query('//*[not(name()="table")]/@*') as $attrNode) {
    $attrNode->ownerElement->removeAttributeNode($attrNode);
}

$output = array();
foreach ($xpath->query('//body/*') as $childNode) {
    $output[] = $dom->saveXml($childNode, LIBXML_NOEMPTYTAG);
}

echo implode("\n", $output);

输出

<p>
  <strong>hello</strong>
</p>
<table style="text-align:center"></table>

If you want to avoid using regex, because you really souldn't use regex to work on xml/html structures, try:

<?php
$text = '<p width="50px;" style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p><table style="text-align:center"></table>';

$dom = new DOMDocument;
$dom->formatOutput = true;
$dom->loadHtml($text);

$xpath = new DOMXpath($dom);
foreach ($xpath->query('//*[not(name()="table")]/@*') as $attrNode) {
    $attrNode->ownerElement->removeAttributeNode($attrNode);
}

$output = array();
foreach ($xpath->query('//body/*') as $childNode) {
    $output[] = $dom->saveXml($childNode, LIBXML_NOEMPTYTAG);
}

echo implode("\n", $output);

Output:

<p>
  <strong>hello</strong>
</p>
<table style="text-align:center"></table>
空城仅有旧梦在 2024-11-21 11:47:07

您与当前的 reg-ex 非常接近。您需要进行检查(在这种情况下认为这是负向预测?)

<(?!table)([az][a-z0-9]*)[^>]*? (\/?)>

reg-ex 的第一部分所做的是检查它是否不以 'table' 开头,那么它就是您的正则表达式。

You are very close with your current reg-ex. You need to do a check (think it is a negative look-ahead in this case?)

<(?!table)([a-z][a-z0-9]*)[^>]*?(\/?)>

What that first bit of reg-ex is doing is checking that it does not start with 'table', then it is your regex.

一张白纸 2024-11-21 11:47:07

有点老套的解决方案,但有效。
尝试在代码中禁用 TABLE 标签一段时间,然后再次启用它们。
它会起作用的。

请参阅:http://codepad.org/nevLWMq8

<?php

$text = '<p width="50px;" style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p><table style="text-align:center"></table>';

/* temporary change table tags with something not occuring in your HTML  */
$textTemp = str_replace(array("<table","/table>"),array('###','+++'),$text);


$text_2 = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', $textTemp);



echo "\n\n";
 /* restore back the table tags */

$finalText =  str_replace(array("###","+++"),array("<table","/table>"),$text_2);
echo $finalText ;

?>

Bit of hacky solution, but works .
Try disabling TABLE tags for a while in your code, and enable them again.
It would work.

see : http://codepad.org/nevLWMq8

<?php

$text = '<p width="50px;" style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p><table style="text-align:center"></table>';

/* temporary change table tags with something not occuring in your HTML  */
$textTemp = str_replace(array("<table","/table>"),array('###','+++'),$text);


$text_2 = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', $textTemp);



echo "\n\n";
 /* restore back the table tags */

$finalText =  str_replace(array("###","+++"),array("<table","/table>"),$text_2);
echo $finalText ;

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文