删除文本中的 HTML 标签和方括号标签,但不删除 PHP 标签或其内容

发布于 2024-11-06 00:52:46 字数 761 浏览 6 评论 0原文

我需要 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 技术交流群。

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

发布评论

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

评论(4

温馨耳语 2024-11-13 00:52:46

当使用 / 作为分隔符和 [] 时,您需要转义 / 等字符,因为它们在正则表达式中具有用途:

$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);

You need to escape chars like / when using / as a delimiter and [] as they have uses in regex:

$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);
℡Ms空城旧梦 2024-11-13 00:52:46

转义你的方括号。在正则表达式中,[] 是指示字符类的标签,并且模式匹配括号内的任何一个字符。

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.

爱格式化 2024-11-13 00:52:46

为什么不采用不同的方法:

获取所有 php 标签和内容

$src = get_the_content();
$matches = array();
preg_match_all('/(<\?php(?:.*)\?>)/i',$src,$matches);
echo implode("\n",$matches);

或获取 [sourcecode] 块的所有内容

$src = get_the_content();
$matches = array();
preg_match_all('/\[sourcecode[^\]]*\](.*)\[\/sourcecode\]/i',$src,$matches);
echo implode("\n",$matches);

why not a different approach:

get all php tags and content

$src = get_the_content();
$matches = array();
preg_match_all('/(<\?php(?:.*)\?>)/i',$src,$matches);
echo implode("\n",$matches);

or get all contents of [sourcecode] blocks

$src = get_the_content();
$matches = array();
preg_match_all('/\[sourcecode[^\]]*\](.*)\[\/sourcecode\]/i',$src,$matches);
echo implode("\n",$matches);
无语# 2024-11-13 00:52:46

您的编码尝试需要的不仅仅是转义对正则表达式引擎具有特殊含义的字符。方括号标签模式需要适应语言值。

strip_tags() 不适用于此任务,因为它将删除 ?> 包装的表达式。

在第一个模式中,删除以字母开头的任何开始或结束的“看起来像标签”的子字符串(在开始的方括号和可选的斜杠之后)。

在第二个模式中,删除以 sourcecode 开头的所有开始或结束方括号标记。

代码:(演示)

echo preg_replace(
         ['#</?[a-z][^>]*>#i', '#\[/?sourcecode[^]]*]#i'],
         '',
         $text
     );

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)

echo preg_replace(
         ['#</?[a-z][^>]*>#i', '#\[/?sourcecode[^]]*]#i'],
         '',
         $text
     );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文