这里有一个简单的例子:
$correction = "game";
$text ="Hello this is an example of a $word that starts with a $dollar sign";
$text = str_replace("'$word",$correct,$text);
如果我回复$text,它会说:你好,这是一个以美元符号开头的游戏示例。这很好。现在我想把任何以美元符号开头的单词都改成游戏因此,它将更改所有以$开头的单词,并将它们变成"game"有什么想法吗?
一个简单的正则表达式就可以做到这一点:
$text = preg_replace("#'$[^'b's]+#", $correction, $text);
您需要一个正则表达式:
$text = preg_replace( '/'$[a-z]+/i', 'game', $text);
类似的东西:
preg_replace("/('w*)$([a-z]+)/i", "$1$correction", $text);
注意:我还没有尝试过,所以我的正则表达式可能有点缺陷。
使用正则表达式和preg_replace
$correction = "game";
$text ="Hello this is an example of a '$word that starts with a '$dollar sign";
$text = preg_replace('/'$'w+/', $correction, $text);
print $text;