删除????或更多preg_replace

时间:2012-11-04 19:51:14

标签: php regex string

我想删除四个或更多问号。

我现在正在使用它:

preg_replace( '{\\?+}', '', $text );

但这确实删除了所有问号。

1 个答案:

答案 0 :(得分:1)

// Use {4,} to indicate 4 or more of the preceding thing
$pattern = '~\?{4,}~';

$str = "I have 3??? and that isn't enough";
$str2 = "I have 5????? and that is enough";

// Won't replace only 3 ?
echo preg_replace($pattern, '', $str);
// I have 3??? and that isn't enough

// Will replace 5 ?
echo preg_replace($pattern, '', $str2);
// I have 5 and that is enough