I need to check format of all string insert.
The format must be word1-word2-number. For example (pippo-pippo-12122018)
How to check if string has this format or not?
Thanks
The most obvious would be to use regex, but since the string is easily parsed and fairly simple to check I suggest using a more conventional method.
This loops the parts of the string and checks if the first two parts is string and the second is a number.
$str = "pippo-pippo-12122018";
$arr = explode("-", $str);
$valid = true;
if(count($arr) >3) $valid = false;
foreach($arr as $key => $val){
if($key<2){
if(!is_string($val)) $valid = false;
}else{
if(!is_numeric($val)) $valid = false;
}
}
var_export($valid);