Skip to content
Advertisement

Check if string have specific number of symbols

In trying to figure out how to do it with preg_match, but I’m left with no clue. So what I’m trying is

$string = 'abc.hello.world.123.hola';

if ($string have 3 or more ".") {
    echo 'true';
}

Advertisement

Answer

I know you said preg_match, but there is a specific PHP function for this. Use substr_count() 🙂

$string = 'abc.hello.world.123.hola';
$dots = substr_count($string, '.');

echo $dots; // output: 4
if ($dots >= 3) {
  // do something
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement