Possible Duplicate:
stripping out all characters from a string, leaving numbers
I开发者_StackOverflow社区 have something produced by an API that looks like: 34 Canadian Dollars
. I'm trying to get rid of the alphabetical characters and keep only the numeric characters. How can I do that?
$result = preg_replace('/[^0-9\.,]/', '', $input);
HTH.
Edited to accept commas and periods.
Given the fact that you're working with currencies and that the number may contain a comma or decimal point, you should use this instead:
preg_match('/([0-9\.,]+)/', $input, $matches);
// Output the amount.
echo $matches[1];
preg_match would be perfect for this.
preg_match("/^([0-9]*) *$/", $inputStr, $results);
echo $results[1];
Also, this online regex tester is a great place to test out other regex patterns.
If it is an integer you are trying to collect you can use (int) $input
Well, i dont know your purpose, but if numbers are always in the beginning of the string, you can use casting.
$int = (int)"34 Canadian Dollars";
OR
$string = "34 Canadian Dollars";
$number = (int)$string
hope that helps. the preg_replace in the other answer will also do the job.
精彩评论