Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-26-2010, 07:00 PM   PM User | #1
DataTalk
Regular Coder

 
Join Date: Nov 2010
Location: Oregon
Posts: 219
Thanks: 19
Thanked 10 Times in 10 Posts
DataTalk is an unknown quantity at this point
echo numeric characters ONLY in a string?

Could someone help with this?

for this line of text charactors and numbers;
Company (100)

how would I write code to echo "only numeric values";

result = 100
DataTalk is offline   Reply With Quote
Old 12-26-2010, 07:42 PM   PM User | #2
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Try this out.
PHP Code:
$str "int(11)";
print 
preg_replace("/([^0-9]+)/","",$str); 
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-27-2010, 06:29 AM   PM User | #3
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Or:
PHP Code:
// remove non-numeric characters
$string preg_replace'/\D/'''$string ); 
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 12-27-2010, 04:15 PM   PM User | #4
DataTalk
Regular Coder

 
Join Date: Nov 2010
Location: Oregon
Posts: 219
Thanks: 19
Thanked 10 Times in 10 Posts
DataTalk is an unknown quantity at this point
Thanks DJCMBear! your suggestion worked in my example....

kbluhm: not sure how your suggestion would apply to this, and will that code remove any and all chatacters other other than numeric?

Code:
$str = $columns[Type];
$size = preg_replace("/([^0-9]+)/","",$str);
echo $size;
DataTalk is offline   Reply With Quote
Old 12-27-2010, 05:51 PM   PM User | #5
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Quote:
Originally Posted by kbluhm View Post
Or:
PHP Code:
// remove non-numeric characters
$string preg_replace'/\D/'''$string ); 
Wouldn't it be this, saying if now digit?
As currently it looks like it's setting all digits to nothing, not 100% sure though.
PHP Code:
// remove non-numeric characters
$string preg_replace'/^\D/'''$string ); 
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-27-2010, 07:25 PM   PM User | #6
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
No, that's wrong. In the above example, the ^ signifies the beginning of the string in that context, so it will remove the first character from the beginning of the string... only if is a non-digit.

\d is a digit, equivalent to [0-9]
\D is a non-digit, equivalent to [^0-9]

Just as:
\w is a word character, equivalent to [A-Za-z0-9_]
\W is a non-word character, equivalent to [^A-Za-z0-9_]
\s is a whitespace character
\S is a non-whitespace character, equivalent to [^\s]

...etc.

So, yes DataTalk, the following code will remove all non-digits from the string:
PHP Code:
$string preg_replace'/\D/'''$string ); 
The original suggestion will work, I just provided a simpler alternative. Learning the ins-and-outs of PCRE will take you a long way.
__________________
ZCE

Last edited by kbluhm; 12-27-2010 at 07:35 PM..
kbluhm is offline   Reply With Quote
Users who have thanked kbluhm for this post:
CloudWriter (12-28-2010)
Old 12-28-2010, 04:28 AM   PM User | #7
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Ahhhhh sorry I missed the caps lol my bad, I get what it's doing now lol.
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:39 PM.


Advertisement
Log in to turn off these ads.