Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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 需要preg_替换的帮助吗 $text='hello; $text\u 2=preg\u replace(“/]*?(\/?)>/i“,”,$text);_Php_Preg Replace - Fatal编程技术网

Php 需要preg_替换的帮助吗 $text='hello; $text\u 2=preg\u replace(“/]*?(\/?)>/i“,”,$text);

Php 需要preg_替换的帮助吗 $text='hello; $text\u 2=preg\u replace(“/]*?(\/?)>/i“,”,$text);,php,preg-replace,Php,Preg Replace,输出(我在这里给出了html格式): $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]*)[^&g

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

$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格式):


你好

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

任何帮助都是感激和感激的


提前感谢…

一点黑客解决方案,但效果不错。 尝试在代码中禁用表标记一段时间,然后再次启用它们。 这会奏效的

见:


有点老套的解决方案,但很管用。
尝试在代码中禁用表标记一段时间,然后再次启用它们。
这会奏效的

见:


您与当前的reg-ex非常接近。您需要做一个检查(在这种情况下,您认为这是一个负面展望?)

]*?(\/?)>


reg-ex的第一部分所做的是检查它是否以“table”开头,然后是您的reg-ex。

您与当前的reg-ex非常接近。您需要进行检查(在这种情况下,这是一个负面展望?)

]*?(\/?)>


reg-ex的第一部分所做的是检查它是否以“table”开头,然后是您的正则表达式。

如果您想避免使用正则表达式,因为您确实不应该使用正则表达式处理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>';

/* 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 ;

?>

如果您想避免使用regex,因为您确实不应该使用regex处理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>';

/* 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 ;

?>

可能重复的可能重复的感谢迪克。。。你用你的回答使我形成。。。。谢谢你,朋友。。。我还有一个小小的疑问。如果我需要在NOT条件下添加两个或多个标签,如table,我该怎么做?…您可以将自己的标签添加到前瞻中,例如
(?!table | div | othertag)
非常感谢您的即时响应Dickie.-)谢谢你,迪克。。。你用你的回答使我形成。。。。谢谢你,朋友。。。我还有一个小小的疑问。如果我需要在NOT条件下添加两个或多个标签,如table,我该怎么做?…您可以将自己的标签添加到前瞻中,例如
(?!table | div | othertag)
非常感谢您的即时响应Dickie.-)感谢您的建议“不要使用正则表达式处理xml/html结构”。但在我的例子中,它只是以PDF格式查看内容。这就是我喜欢的…谢谢你的建议“不要使用正则表达式来处理xml/html结构”。但在我的例子中,它只是以PDF格式查看内容。这就是我的目的。。。
<?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 ;

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