str_replace 内容未替换
str_replace with content not replacing
堆垛机你好,
我只是有一个关于 str_replace()
函数的 PHP 小问题。当我替换某些东西时,它会替换所有东西;没关系。但我想知道的是:
str_replace("*", "<strong>", $message);
是否可以将 str_replace 用于类似 * This content is Bold * 的代码,只有内容,但仍然用 <strong>
和 </strong>
替换星号?
示例:
原文:**This Should be Bold**
替换后:<strong>This Should be Bold</strong>
对于将其标记为重复的人:这不是关闭 HTML 标签,而是替换。
我希望我没有那么不清楚。谢谢。
改用正则表达式;更方便:
$message = "**This Should be Bold**";
$message = preg_replace('#\**([^\*]+)\**#m', '<strong></strong>', $message);
echo $message;
或者如果您想将小行星的数量限制为 2 个:
'#\*{1,2}([^\*]+)\*{1,2}#m'
你也可以这样做
<?php
$string = '**This Should be Bold**';
$string = preg_replace("/\*\*(.+?)\*\*/", "<strong></strong>", $string);
echo $string;
?>
堆垛机你好,
我只是有一个关于 str_replace()
函数的 PHP 小问题。当我替换某些东西时,它会替换所有东西;没关系。但我想知道的是:
str_replace("*", "<strong>", $message);
是否可以将 str_replace 用于类似 * This content is Bold * 的代码,只有内容,但仍然用 <strong>
和 </strong>
替换星号?
示例:
原文:**This Should be Bold**
替换后:<strong>This Should be Bold</strong>
对于将其标记为重复的人:这不是关闭 HTML 标签,而是替换。
我希望我没有那么不清楚。谢谢。
改用正则表达式;更方便:
$message = "**This Should be Bold**";
$message = preg_replace('#\**([^\*]+)\**#m', '<strong></strong>', $message);
echo $message;
或者如果您想将小行星的数量限制为 2 个:
'#\*{1,2}([^\*]+)\*{1,2}#m'
你也可以这样做
<?php
$string = '**This Should be Bold**';
$string = preg_replace("/\*\*(.+?)\*\*/", "<strong></strong>", $string);
echo $string;
?>