FuZion 05-12-2008, 11:21 PM Hi,
I need some RegEx that will take a string, for example .5 gal or 1.25 qts, and remove everything but the numbers from the string.
Is this possible?
thank you!
logictrap 05-12-2008, 11:43 PM $y=ereg_replace("[^0-9]", "", $x);
oesxyl 05-12-2008, 11:46 PM Hi,
I need some RegEx that will take a string, for example .5 gal or 1.25 qts, and remove everything but the numbers from the string.
Is this possible?
thank you!
$result = preg_replace("/\D/","",$phone);
regards
FuZion 05-13-2008, 12:40 AM Will those keep the decimals as well?
Thanks guys!
Mwnciau 05-13-2008, 01:25 AM For decimals you can do this:
$string = 'fdsgd3.141asdggfgfdsdniojn';
echo preg_replace ( '#\D*?(\d+(\.\d+)?)\D*#', '$1', $string );
// Outputs 3.141
FuZion 05-13-2008, 02:01 AM Perfect. Thank you!
FuZion 05-13-2008, 04:29 AM Well, it's working great except for one thing. If the number starts with a decimal, it doesn't keep the decimal. How do I fix this?
I really appreciate your help!
Mwnciau 05-13-2008, 01:31 PM preg_replace ( '#\D*?(\d*(\.\d+)?)\D*#', '$1', $string );
:)
FuZion 05-13-2008, 02:19 PM Ok, well I tried that one, and it rounded my .5 up to one. Is there something on the server that I need to change?
This is exactly what the string is: .5 gal
But, when I insert it into the database it changes it to 1 now. Any ideas?
I really appreciate all the help, thank you!
FuZion 05-13-2008, 02:20 PM Just a note, the string wont always be what I posed above, it could be 1.24 qts or 56.77 lbs.
logictrap 05-13-2008, 02:22 PM Try this:
$result=ereg_replace("[^.0-9]", "", $x);
aedrin 05-13-2008, 04:01 PM Do not use ereg_replace(), it has been deprecated for several versions. PHP6 won't even contain the ereg_* functions, so it would be setting yourself up for failure to use them.
logictrap 05-13-2008, 06:33 PM Thanks - I was not aware of that.
Try this:
<?php
$result=preg_replace("/[^0-9.]/", "", $input);
?>
FuZion 05-15-2008, 12:10 AM Thanks guys I appreciate it!
kbluhm 05-15-2008, 04:28 PM You have to escape the decimal or it will be read as a wildcard.
$input = preg_replace( '/[^\d\.]/', '', $input );
|
|