Fra*_*isc 27 php replace case-insensitive match
如何使用str_ireplace(或类似的东西)替换某些文本进行格式化,然后使用相同的大写字母返回?
例:
$original="The quick red fox jumps over the lazy brown dog.";
$find="thE";
print str_ireplace($find,'<b>'.$find.'</b>',$original);
Run Code Online (Sandbox Code Playgroud)
这将输出: 在快红狐狸跳过懒惰的黄狗.
我希望它保留原始案例,并且仅应用格式,在此示例中为粗体文本.
谢谢.
Art*_*cto 41
$original = "The quick red fox jumps over the lazy brown dog.";
$new = preg_replace("/the/i", "<b>\$0</b>", $original);
Run Code Online (Sandbox Code Playgroud)
给" 的快速红狐狸跳过了懒惰的棕色狗." 如果要匹配特定单词,可以添加单词边界:preg_replace('/\bthe\b/i', ...
.
如果要参数化替换,可以使用preg_quote
:
preg_replace('/\b' . preg_quote($word, "/") . '\b/i', "<b>\$0</b>", $original);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7822 次 |
最近记录: |