使用preg_replace将一个字符的多个实例转换为仅一个(aa=a)


convert multiple instances of a character to only one (aa = a) using preg_replace?

我有一个字符串,我想将-的多个外观转换为一个-

我试过preg_replace('/--+/g', '-', $string),但没有任何结果。。

您不应该在模式中使用g,并且您可以简化您的正则表达式:

preg_replace('/-+/', '-', $string);

不需要后间隙转义。

Onhttp://ideone.com/IOlpv:

<?
$string = "asdfsdfd----sdfsdfs-sdf-sdf";
echo preg_replace('/-+/', '-', $string);
?>

输出:

asdfsdfd-sdfsdfs-sdf-sdf
preg_replace('/(['-]+)/', '-', $string)

您的代码给出以下错误:

警告:preg_replace():未知修饰符"g"

没有g修饰符。尝试:

preg_replace('/--+/', '-', $string)
相关文章: