Skip to main content
Inspiring
February 21, 2008
Question

Stripping numeric data from a string in PHP?

  • February 21, 2008
  • 13 replies
  • 686 views
I have an input field that *could* get either pure numeric info, or
something like $###,###. What would I use to strip that additional string
stuff from that field?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


This topic is closed to new replies. Start a new post to keep the conversation going.

13 replies

Inspiring
February 21, 2008
.oO(Murray *ACE*)

>I have an input field that *could* get either pure numeric info, or
>something like $###,###. What would I use to strip that additional string
>stuff from that field?

Do you really just want to strip all non-numeric chars, even if it may
completely change the number? Nothing easier than that with a regex:

$value = preg_replace('/[^[:digit:]]/', '', $value);

should strip all chars that are no digits.

Micha
Inspiring
February 21, 2008
OK - this works -

$value = str_replace(",","",str_replace("$","",$value));

I suppose I could do -

$search = array("$",",");
$replace = array("","");
$value = str_replace($search,$replace,$value);

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:fpk5it$ic2$1@forums.macromedia.com...
> Aha - I could just replace those characters with null using strtr($value,
> '$,',','), no?
>
> --
> Murray --- ICQ 71997575
> Adobe Community Expert
> (If you *MUST* email me, don't LAUGH when you do so!)
> ==================
> http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
> http://www.dwfaq.com - DW FAQs, Tutorials & Resources
> ==================
>
>
> "Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
> news:fpk5ce$i37$1@forums.macromedia.com...
>>I have an input field that *could* get either pure numeric info, or
>>something like $###,###. What would I use to strip that additional string
>>stuff from that field?
>>
>> --
>> Murray --- ICQ 71997575
>> Adobe Community Expert
>> (If you *MUST* email me, don't LAUGH when you do so!)
>> ==================
>> http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
>> http://www.dwfaq.com - DW FAQs, Tutorials & Resources
>> ==================
>>
>>
>

Inspiring
February 21, 2008
Aha - I could just replace those characters with null using strtr($value,
'$,',','), no?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:fpk5ce$i37$1@forums.macromedia.com...
>I have an input field that *could* get either pure numeric info, or
>something like $###,###. What would I use to strip that additional string
>stuff from that field?
>
> --
> Murray --- ICQ 71997575
> Adobe Community Expert
> (If you *MUST* email me, don't LAUGH when you do so!)
> ==================
> http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
> http://www.dwfaq.com - DW FAQs, Tutorials & Resources
> ==================
>
>