Re: Re: Extracting Numbers from a string.
| From: | Brad Bonkoski | Date: | Wed, 11 Sep 2002 22:44:30 +0000 |
| Subject: | Re: Re: Extracting Numbers from a string. | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-116054@lists.php.net to get a copy of this message | ||
Of course they may not always know that the user would insert a '$' or a
'.' which is what you are keying off of. I am not all that familiar
with ereg* functions, I might attack it with stepping through charater
by charater and is is_numeric($c) returns true, place it applend it to
another variable, which at the end would have strictly the numbers...
That would be my initial approach, but I'm sure there are many,many
others...
-Brad
M1tch wrote:
>
> it doesn't use ereg, but it (should) would work:
>
> $mystring = "I have $56.55 dollars, don't you know";
> $mystring = substr($mystring, strpos($mystring, "$")); //strip out after
> the $ sign
> $mystring = substr($mystring, 0, strpos($mystring, " ")); //keep only
> till first space
>
> //mystring now contains $56.55
> if you want it split into two variables, of '$56', and '55' use:
> $a = explode(".", $mystring);
> //$a[0'] = $56
> //$a[1] = 55
>
> (by the way, there may be functions that work better than using the
> substr+strpos functions, I just can't remember off the top of my head!)
>
> Of course, ereg might well work better!
>
> "Jason Caldwell" <jason@thinkingman.org> wrote in message
> news:20020911223123.76157.qmail@pb1.pair.com...
> > I need to extract the numbers only from a field.
> >
> > For example: I have an AMOUNT field and so that I can filter out any user
> > typo's I would like to extract the numbers only.
> >
> > If the user enters $56.55 for example or just $56 then I would like to be
> > able to remove the "$" and the "." keeping just the 56 or 5655.
> >
> > Can I use eregi_replace() to do this -- I've been trying but it doesn't
> seem
> > to work right.
> >
> > Thanks.
> > Jason
> >
> >
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php