如何删除括号[]内的HTML标签?
How to remove HTML tags inside of brackets[]?
我有这样的字符串:
[<span style="font-size: 12.1599998474121px; line-height: 15.8079996109009px;">heading </span>heading="h1"]Its a <span style="text-decoration: line-through;">subject</span>.[/<span style="font-size: 12.1599998474121px; line-height: 15.8079996109009px;">heading</span>]
我想使用 PHP preg_replace 等删除括号内的 HTML 标签。最终字符串应该是这样的:
[heading heading="h1"]Its a <span style="text-decoration: line-through;">subject</span>.[/heading]
我进行了很多搜索以找到解决方案,但没有成功。
取决于你想删除多少。
示例:
Pattern: '<.*?>'
Result: [heading heading="h1"]Its a subject.[/heading]
但是从您的回答来看,您希望保留标题内的 html 标签。我不明白,究竟基于哪个规则?为什么这是一个例外?
这应该适合你:
这里我只是在你的字符串的每个括号中使用 strip_tags()
和 return 它。
echo $str = preg_replace_callback("/\[(.*?)\]/", function($m){
return strip_tags($m[0]);
}, $str);
您可以使用 callback with the following regular expression and utilize strip_tags()
...
$str = preg_replace_callback('~\[[^]]*]~',
function($m) {
return strip_tags($m[0]);
}, $str);
您可以使用单个正则表达式来获得您想要的内容:
$re = "#][^\[\]]*(*SKIP)(*F)|<\/?[a-z].*?>#si";
$str = "[<span style=\"font-size: 12.1599998474121px; line-height: 15.8079996109009px;\">heading </span>heading=\"h1\"]Its a <span style=\"text-decoration: line-through;\">subject</span>.[/<span style=\"font-size: 12.1599998474121px; line-height: 15.8079996109009px;\">heading</span>]";
$result = preg_replace($re, '', $str);
echo $result;
sample code 的输出:
[heading heading="h1"]Its a <span style="text-decoration: line-through;">subject</span>.[/heading]
我有这样的字符串:
[<span style="font-size: 12.1599998474121px; line-height: 15.8079996109009px;">heading </span>heading="h1"]Its a <span style="text-decoration: line-through;">subject</span>.[/<span style="font-size: 12.1599998474121px; line-height: 15.8079996109009px;">heading</span>]
我想使用 PHP preg_replace 等删除括号内的 HTML 标签。最终字符串应该是这样的:
[heading heading="h1"]Its a <span style="text-decoration: line-through;">subject</span>.[/heading]
我进行了很多搜索以找到解决方案,但没有成功。
取决于你想删除多少。
示例:
Pattern: '<.*?>'
Result: [heading heading="h1"]Its a subject.[/heading]
但是从您的回答来看,您希望保留标题内的 html 标签。我不明白,究竟基于哪个规则?为什么这是一个例外?
这应该适合你:
这里我只是在你的字符串的每个括号中使用 strip_tags()
和 return 它。
echo $str = preg_replace_callback("/\[(.*?)\]/", function($m){
return strip_tags($m[0]);
}, $str);
您可以使用 callback with the following regular expression and utilize strip_tags()
...
$str = preg_replace_callback('~\[[^]]*]~',
function($m) {
return strip_tags($m[0]);
}, $str);
您可以使用单个正则表达式来获得您想要的内容:
$re = "#][^\[\]]*(*SKIP)(*F)|<\/?[a-z].*?>#si";
$str = "[<span style=\"font-size: 12.1599998474121px; line-height: 15.8079996109009px;\">heading </span>heading=\"h1\"]Its a <span style=\"text-decoration: line-through;\">subject</span>.[/<span style=\"font-size: 12.1599998474121px; line-height: 15.8079996109009px;\">heading</span>]";
$result = preg_replace($re, '', $str);
echo $result;
sample code 的输出:
[heading heading="h1"]Its a <span style="text-decoration: line-through;">subject</span>.[/heading]