I had a look at regular expressions in PHP but I don't really understand how they work.
I have various strings like "1-title" , "1-1-secondTitle", "1-2-otherTitle", This goes up to three level ("1-2-1-text"), so on every string I have this formating and I would like to check if there is one, two or three number before the string starts and then output "0", "1" or "2".
So to make it clearer
"1-index" should return "0"
"3-1-text" should return "1"
"5-2-1-otherTitle" should return "2"
is it possible to check the number of char before the first letter on a string?
Alternaatively, if you dont want to use regex, then, you could just iterate thru the string and overwrite the index. Consider this example:
$string = '5-2-1-otherTitle';
$string = str_ireplace('-', '', $string);
$index = null;
for($x = 0; $x < strlen($string); $x++) {
if(is_numeric($string[$x])) {
$index = $x;
}
}
echo $index; // outputs 2
// works
// echo preg_match_all("/[0-9]/", $string) - 1;