php替换strong标签,php-从html标记中删除所有属性

php-从html标记中删除所有属性

我有这个HTML代码:

hello

但它应该变成(对于所有可能的html标签):

hello

Andres SK asked 2020-08-02T20:33:57Z

9个解决方案

144 votes

改编自我对类似问题的回答

$text = '

hello

';

echo preg_replace("/]*?(\/?)>/i",'', $text);

//

hello

RegExp细分:

/ # Start Pattern

< # Match '

( # Start Capture Group $1 - Tag Name

[a-z] # Match 'a' through 'z'

[a-z0-9]* # Match 'a' through 'z' or '0' through '9' zero or more times

) # End Capture Group

[^>]*? # Match anything other than '>', Zero or More times, not-greedy (wont eat the /)

(\/?) # Capture Group $2 - '/' if it is there

> # Match '>'

/i # End Pattern - Case Insensitive

添加一些引号,并使用替换文本

,它应该删除标记名之后的所有文本,直到标记

">或>的末尾。

请注意,这不一定适用于所有输入,因为Anti-HTML + RegExp会告诉您。 有一些后备功能,最明显的是

最终会成为

">和其他一些坏的问题...我建议将Zend_Filter_StripTags视为PHP中更完整的证明标签/属性过滤器

gnarf answered 2020-08-02T20:34:19Z

66 votes

这是使用本地DOM的方法:

$dom = new DOMDocument; // init new DOMDocument

$dom->loadHTML($html); // load HTML into it

$xpath = new DOMXPath($dom); // create a new XPath

$nodes = $xpath->query('//*[@style]'); // Find elements with a style attribute

foreach ($nodes as $node) { // Iterate over found elements

$node->removeAttribute('style'); // Remove style attribute

}

echo $dom->saveHTML(); // output cleaned HTML

如果要从所有可能的标记中删除所有可能的属性,请执行

$dom = new DOMDocument;

$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$nodes = $xpath->query('//@*');

foreach ($nodes as $node) {

$node->parentNode->removeAttribute($node->nodeName);

}

echo $dom->saveHTML();

Gordon answered 2020-08-02T20:34:44Z

9 votes

我会避免使用正则表达式,因为HTML不是常规语言,而是使用诸如Simple HTML DOM之类的html解析器

您可以使用attr获得对象具有的属性的列表。例如:

$html = str_get_html('

World
');

var_dump($html->find("div", 0)->attr); /

/*

array(1) {

["id"]=>

string(5) "hello"

}

*/

foreach ( $html->find("div", 0)->attr as &$value ){

$value = null;

}

print $html

//

World

Yacoby answered 2020-08-02T20:35:08Z

4 votes

$html_text = '

Hello world. Its beautiful day.

';

$strip_text = strip_tags($html_text, '');

$result = preg_replace('/]*>/', '', $strip_text);

echo $result;

// Result

string 'Hello world. Its beautiful day.'

TobiasDeVil answered 2020-08-02T20:35:23Z

1 votes

正则表达式对于HTML解析来说太脆弱了。 在您的示例中,以下内容将删除您的属性:

echo preg_replace(

"|/]+)?|",

"

"

\nhello\n

\n"

);

更新资料

将第二个捕获设为可选,并且不要在结束标记中去除“ /”:

|]+)|至|/]+)?|

演示此正则表达式的工作原理:

$ phpsh

Starting php

type 'h' or 'help' to see instructions & features

php> $html = '

hello

';

php> echo preg_replace("|/]+)?|", "

hello

php> $html = 'hello';

php> echo preg_replace("|/]+)?|", "

hello

Greg K answered 2020-08-02T20:36:01Z

1 votes

要具体执行andufo想要的,只需:

$html = preg_replace( "#(]+>#", "\\1>", $html );

也就是说,他想从开始标签中除去标签名称以外的任何内容。 当然,它不适用于自动关闭标签。

Sp4cecat answered 2020-08-02T20:36:25Z

1 votes

希望这可以帮助。 这可能不是最快的方法,尤其是对于大型html块。如果有人有任何建议可以使此操作更快,请告诉我。

function StringEx($str, $start, $end)

{

$str_low = strtolower($str);

$pos_start = strpos($str_low, $start);

$pos_end = strpos($str_low, $end, ($pos_start + strlen($start)));

if($pos_end==0) return false;

if ( ($pos_start !== false) && ($pos_end !== false) )

{

$pos1 = $pos_start + strlen($start);

$pos2 = $pos_end - $pos1;

$RData = substr($str, $pos1, $pos2);

if($RData=='') { return true; }

return $RData;

}

return false;

}

$S = ''; while($RData=StringEx($DATA, $S, $E)) { if($RData==true) {$RData='';} $DATA = str_ireplace($S.$RData.$E, '||||||', $DATA); } $DATA = str_ireplace('||||||', $S.$E, $DATA);

Brandon Orth answered 2020-08-02T20:36:45Z

0 votes

$text = '

Test paragraph.

Other text';

echo strip_tags($text);

echo "\n";

// Allow

and

echo strip_tags($text, '

');

?>

Tizón answered 2020-08-02T20:37:01Z

0 votes

这是摆脱属性的一种简单方法。 它可以很好地处理格式错误的html。

$string = '

hello

//get all html elements on a line by themselves

$string_html_on_lines = str_replace (array(""),array("\n\n"),$string);

//find lines starting with a '

$string_attribute_free = preg_replace("/\n(",$string_html_on_lines);

echo $string_attribute_free;

?>

Greg Randall answered 2020-08-02T20:37:20Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值