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;
精彩评论