Can someone help me with algorithm for finding the position of the first occurrence of any number in a string?
The code I found on the web does not work:
function my_offset($text){
preg_match('/^[^\-]*-\D*/', $text, $m);
return strlen($m[0]);
}
echo my_offset('[HorribleSubs] Bleach - 311 [720p].mkv');
should work for this. The original code required a
-
to come before the first number, perhaps that was the problem?The built-in PHP function
strcspn()
will do the same as the function in Stanislav Shabalin's answer when used like so:Examples:
HTH
I can do regular expressions but I have to go into an altered state to remember what it does after I've coded it.
Here is a simple PHP function you can use...
Problem
Find the first occurring number in a string
Solution
Here is a non regex solution in javascript
Example Input
Output