Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Stripping numeric data from a string in PHP?

LEGEND ,
Feb 21, 2008 Feb 21, 2008
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
==================


TOPICS
Server side applications
681
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 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
> ==================
>
>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 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
>> ==================
>>
>>
>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 21, 2008
> 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:
>

Thanks. Didn't even think of that. I'm not so worried about someone
inputting 2.456^10 or something. The field is for a mortgage amount or a
loan amount....

--
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
==================


"Michael Fesser" <netizen@gmx.de> wrote in message
news:pnerr39m2hdmv7t6ns0r5lph5m574gt8nr@4ax.com...
> .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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 21, 2008
Michael Fesser wrote:
> $value = preg_replace('/[^[:digit:]]/', '', $value);
>
> should strip all chars that are no digits.

That would also remove a decimal point, potentially making nonsense of
the number. This preserves the number correctly.

$value = preg_replace('/[^\d.]/', '', $value);

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 21, 2008
Yes. Although it's not likely I will get any decimal values....

--
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
==================


"David Powers" <david@example.com> wrote in message
news:fpkerh$rqv$1@forums.macromedia.com...
> Michael Fesser wrote:
>> $value = preg_replace('/[^[:digit:]]/', '', $value);
>>
>> should strip all chars that are no digits.
>
> That would also remove a decimal point, potentially making nonsense of the
> number. This preserves the number correctly.
>
> $value = preg_replace('/[^\d.]/', '', $value);
>
> --
> David Powers, Adobe Community Expert
> Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
> Author, "PHP Solutions" (friends of ED)
> http://foundationphp.com/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 21, 2008
On 21 Feb 2008 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:

> Yes. Although it's not likely I will get any decimal values....

You must have a high opinion of the site's likely users?

"Every day, man is making bigger and better fool-proof things, and
every day, nature is making bigger and better fools. So far, I
think nature is winning."
-- Albert Einstein

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/contact.php
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 21, 2008
8)

--
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
==================


"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns9A4B9381D343CmakowiecatnycapdotrE@216.104.212.96...
> On 21 Feb 2008 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>
>> Yes. Although it's not likely I will get any decimal values....
>
> You must have a high opinion of the site's likely users?
>
> "Every day, man is making bigger and better fool-proof things, and
> every day, nature is making bigger and better fools. So far, I
> think nature is winning."
> -- Albert Einstein
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/contact.php

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 21, 2008
Actually, my method only strips "$" and "," so I am safe, I think, in the
event someone enters a mortgage value of "$1,000,000.50"....

--
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:fpkjsm$4cv$1@forums.macromedia.com...
> 8)
>
> --
> 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
> ==================
>
>
> "Joe Makowiec" <makowiec@invalid.invalid> wrote in message
> news:Xns9A4B9381D343CmakowiecatnycapdotrE@216.104.212.96...
>> On 21 Feb 2008 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>>
>>> Yes. Although it's not likely I will get any decimal values....
>>
>> You must have a high opinion of the site's likely users?
>>
>> "Every day, man is making bigger and better fool-proof things, and
>> every day, nature is making bigger and better fools. So far, I
>> think nature is winning."
>> -- Albert Einstein
>>
>> --
>> Joe Makowiec
>> http://makowiec.net/
>> Email: http://makowiec.net/contact.php
>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 21, 2008
On 21 Feb 2008 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:

> Actually, my method only strips "$" and "," so I am safe, I think,
> in the event someone enters a mortgage value of "$1,000,000.50"....

Hey, somebody out there's gonna think that they have to enter a decimal
point and two zeros for a currency value. And if you do strip decimals,
they're going to think that they can't afford $58,357/month...

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/contact.php
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 21, 2008
8)

--
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
==================


"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns9A4B9FA37C35CmakowiecatnycapdotrE@216.104.212.96...
> On 21 Feb 2008 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>
>> Actually, my method only strips "$" and "," so I am safe, I think,
>> in the event someone enters a mortgage value of "$1,000,000.50"....
>
> Hey, somebody out there's gonna think that they have to enter a decimal
> point and two zeros for a currency value. And if you do strip decimals,
> they're going to think that they can't afford $58,357/month...
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/contact.php

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 21, 2008
Murray *ACE* wrote:

> Yes. Although it's not likely I will get any decimal values....
>
Yes, but if it has two digits after the decimal point, the resultant
number will be greater by a factor of 100,
20,000.55 becomes 2,000,055, for ex.

Mick
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 21, 2008 Feb 21, 2008
LATEST
Sure. However, since I am not stripping decimals, I ain't gonna worry about
it! 8)

--
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
==================


"Mick White" <himselfBOGUS@mickweb.com> wrote in message
news:fpl1cg$i2c$2@forums.macromedia.com...
> Murray *ACE* wrote:
>
>> Yes. Although it's not likely I will get any decimal values....
>>
> Yes, but if it has two digits after the decimal point, the resultant
> number will be greater by a factor of 100,
> 20,000.55 becomes 2,000,055, for ex.
>
> Mick

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines