我有一个奇怪的场景,如果我使用下面的代码,任何帮助都将不胜感激,我正在尝试在标题上添加h1标记
$phrase = "*title** Some text goes here.";
$target = ['*', '**'];
$replace = ["<h1>", "</h1>"];
$newPhrase = str_replace($target, $replace, $phrase);
echo $newPhrase;
输出的HTML如下所示:
<h1>title</h1>
<h1></h1>
<h1> Some text goes here.</h1>
但如果我使用以下内容(与之前相同,但*替换为1,**替换为2):
$phrase = "1title2 Some text goes here.";
$target = ['1', '2'];
$replace = ["<h1>", "</h1>"];
$newPhrase = str_replace($target, $replace, $phrase);
echo $newPhrase;
它起作用了,我得到了输出:
<h1>title</h1>
Some text goes here. (not as a H1 element)
谁能解释一下吗?为什么要这样对待*角色?我能解决这个问题,这样我就能使用它们了吗?
谢谢