开发者

PHP Check if string has consecutive underscores

How can I check if a s开发者_如何学JAVAtring contains two or more consecutive underscore(_) characters, eg. __ or ____ etc?


if you want just to check, without performing any action, you can use strpos

if (strpos($string, '__') !== false)

However, if you want to replace them with single underscore for example, you need preg_replace

$string = preg_replace('/_{2,}/', '_', $string);

here {2,} means 2 or more


if (preg_match('~__~', $string)){
    echo "has two or more underscores";
}


if (strpos($string,"__")!==false)


echo strpos($string, '__') !== FALSE;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜