regex - What it the fastest way to check if a string consists of three or more dashes in php? -


is regex way? slow?

something this?

preg_match("/^(\-){3,}/", $string); 

only dashes

if want string only dashes, , there must 3 or more:

$match = (preg_match('/^-{3,}$/', $string) === 1); 

another way without regex seems 25% slower (isset beats strlen):

$match = (count_chars($string, 3) === '-' && isset($string[2])); 

adjacent dashes

if want 3 or more dashes in row, there may other characters (e.g. foo---bar):

$match = (strpos($string, '---') !== false); 

some dashes

if want 3 or more dashes anywhere (e.g. -foo-bar-):

$match = (substr_count($string, '-') >= 3); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -