I have a variable that contains a string of alpha and numeric characters, such as "Om3745". Is there a simple way in PHP to remove the alpha characters from the string? I've tried using substr(), but since the number of alpha characters before the number varies, it doesn't work consistently.
Thanks for any help.
Bill
Removing alpha characters from string
Moderator: General Moderators
Re: Removing alpha characters from string
Looks like you want to use a RegEx for this. Try preg_replace().
this will replace anything that isnt a number with "".
Code: Select all
$new_string = preg_replace("/[^1234567890]/", "", "Om3745");- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Removing alpha characters from string
Code: Select all
preg_replace("/[^\d]/", "", "Om3745");