如何用字符串中的特殊符号(。)替换单词?

问题描述 投票:2回答:3

我有样本字符串:

$string = "муқ. - муқоиса муқ. муқ.шавад муқ томуқ.";

我试着用我的代码:

$result = preg_replace("/\b(муқ\.?)\b/u", 'repl', $string);
echo "$result";

结果:repl。 - 比较repl。替换更换

需要的结果:将比较替换为另一个。

在这里,我无法用“。”代替单词。结束了符号!

php string preg-replace
3个回答
4
投票

试试这个:

$result = preg_replace("/\bмуқ\.\B/u", "repl", $string);

共享链接:https://regex101.com/r/zPXOtP/1


1
投票

使用否定前瞻:

$result = preg_replace("/\bмуқ\.(?!\w)/u", 'repl', $string);

-2
投票

您可以使用str_replace函数。

str_replace(".", "your word", "your string");
© www.soinside.com 2019 - 2024. All rights reserved.