Re: convert string to number
| From: | David Otton | Date: | Fri, 16 May 2003 16:01:36 +0000 |
| Subject: | Re: convert string to number | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-147911@lists.php.net to get a copy of this message | ||
On Fri, 16 May 2003 17:24:04 +0200, you wrote:
>is there a function to only find the numeric part of a string, e.g.
>it would convert "4 stars" to 4
Format the string to remove all non-numeric characters, then use
intval() (or maybe floatval())
>and what about "four stars" to 4?
Yeuch. Replace "one" with 1, "two" with 2, etc., then proceed as above.
I assume you're trying to clean up data about hotel/restaurant ratings?
That should be easier... you don't have to deal with anything over 5.
$s = strtolower("four stars");
$s = str_replace("zero", "0", $s);
$s = str_replace("one", "1", $s);
$s = str_replace("two", "2", $s);
$s = str_replace("three", "3", $s);
$s = str_replace("four", "4", $s);
$s = str_replace("five", "5", $s);
$s = ereg_replace("[^0-5]", "", $s);
echo intval($s);