Getting number from string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Getting number from string

Post by winsonlee »

Code: Select all

$string = ''RD0394D";
How can I extract the number out from the string ?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You could try just casting it to an integer:

Code: Select all

$string = (int)"RD0394D";
// or use the function
$string = intval("RD0394D");
You could use a regular expression to remove all characters that are not numbers:

Code: Select all

$string = ''RD0394D";
$number = preg_replace('/[^0-9]/', '', $string);
(#10850)
Post Reply