删除文本中的 HTML 标签和方括号标签,但不删除 PHP 标签或其内容
我需要 preg_replace()
方面的帮助。见下文:
<html>[sourcecode language='php']<?php echo "hello world"; ?>[/sourcecode]</html>
我只希望它显示 PHP 标签并去掉其余部分,所以我会得到以下结果:
<?php echo "hello world"; ?>
我尝试了以下方法:
$update = get_the_content();
$patterns = array();
$patterns[0] = '/<html>/';
$patterns[1] = '/</html>/';
$patterns[2] = '/[sourcecode language]/';
$patterns[3] = '/[/sourcecode]/';
$replacements = array();
$replacements[0] = '';
$replacements[1] = '';
$replacements[2] = '';
$replacements[3] = '';
echo preg_replace($patterns, $replacements, $update);
但它不起作用。我的问题还在于,该语言可能并不总是 PHP。
I need help with preg_replace()
. See below:
<html>[sourcecode language='php']<?php echo "hello world"; ?>[/sourcecode]</html>
I only want it to display the PHP tags and strip the rest out, so I would get the following result:
<?php echo "hello world"; ?>
I have tried the following:
$update = get_the_content();
$patterns = array();
$patterns[0] = '/<html>/';
$patterns[1] = '/</html>/';
$patterns[2] = '/[sourcecode language]/';
$patterns[3] = '/[/sourcecode]/';
$replacements = array();
$replacements[0] = '';
$replacements[1] = '';
$replacements[2] = '';
$replacements[3] = '';
echo preg_replace($patterns, $replacements, $update);
But it doesn't work. My issue, also, is that the language might not always be PHP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当使用 / 作为分隔符和 [] 时,您需要转义 / 等字符,因为它们在正则表达式中具有用途:
You need to escape chars like / when using / as a delimiter and [] as they have uses in regex:
转义你的方括号。在正则表达式中,
[
和]
是指示字符类的标签,并且模式匹配括号内的任何一个字符。Escape your square-brackets. In regular expressions,
[
and]
are the tags that indicate a character class, and the pattern is matching any one of those characters within the brackets.为什么不采用不同的方法:
获取所有 php 标签和内容
或获取 [sourcecode] 块的所有内容
why not a different approach:
get all php tags and content
or get all contents of [sourcecode] blocks
您的编码尝试需要的不仅仅是转义对正则表达式引擎具有特殊含义的字符。方括号标签模式需要适应语言值。
strip_tags()
不适用于此任务,因为它将删除和
?>
包装的表达式。在第一个模式中,删除以字母开头的任何开始或结束的“看起来像标签”的子字符串(在开始的方括号和可选的斜杠之后)。
在第二个模式中,删除以
sourcecode
开头的所有开始或结束方括号标记。代码:(演示)
Your coding attempt needs more than just escaping of characters with special meaning to the regex engine. The square braced tag pattern needs to accommodate the language value.
strip_tags()
is not usable for this task because it will remove the<?php
and?>
wrapped expression.In the first pattern, remove any opening or closing "tag-looking" substring that starts with a letter (after the opening square brace and optional slash).
In the second pattern, remove any opening or closing square-braced tag that starts with
sourcecode
.Code: (Demo)