Question

I have to cut the first part of my string before any number found. For example:

string: "BOBOSZ 27A lok.6" should be cutted to 'BOBOSZ "

string: "aaa 43543" should be cutted to "aaa "

string: "aa2bhs2" should be cutted to "aa"

Im trying with preg_split and explode funcionts but i can't get the right result for now.

Thanks in advance !

Was it helpful?

Solution

You can use this pattern with the preg_match() function:

preg_match('/^[^0-9]+/', $str, $match);
print_r($match);

pattern details:

^         # anchor: start of the string
[^0-9]+   # negated character class: all that is not a digit one or more times

note: you can replace + by * if you consider that an empty string is a valid result.

If you absolutly want to use the preg_split() function, you do:

$result = preg_split('/(?=(?:[^0-9].*)?$)/s', $str);
echo $result[0];

OTHER TIPS

preg_match('#(\w+)\s?\d+#', $string, $match);

You should get $match[1] as I remember :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top