Huang Lun
Huang Lun

Reputation: 21

php remove the leftmost number in string

For some reason, I want to remove the leftmost number inside the whole string. Here are the example:

Is it possible do in php?

I tried to use:

$words = preg_replace('/[0-9]+[a-z]/', '', $file);

but it will remove both the number and the first alphabet.

Upvotes: 0

Views: 45

Answers (2)

user557846
user557846

Reputation:

and if you dont like Rizier123 aswer:

$words = ltrim('123ab1ab123','0123456789');

or

$words = ltrim('123ab1ab123','0..9');

trims all numbers from left, probably faster than are regular expression also

Upvotes: 2

Rizier123
Rizier123

Reputation: 59701

You can just use an anchor and remove the [a-z] part from your regex:

$words = preg_replace('/^[0-9]+/', '', $file);

Upvotes: 4

Related Questions