Current location: Hot Scripts Forums » Programming Languages » PHP » Return all numbers from a string ?


Return all numbers from a string ?

Reply
  #1 (permalink)  
Old 01-03-09, 04:00 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Return all numbers from a string ?

Can anybody tell me some way to return all numbers from a string such as : 99, 0100, 1.2345 ... ? Thanks so much
Reply With Quote
  #2 (permalink)  
Old 01-03-09, 05:04 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
Do you mean you have a string separated with commas in the format number,number,number ?

If so, then the following is for you

PHP Code:

$string "99,0100,1.2345";

$list explode(','$string);

for (
$i 0$i count($list); $i++) {
     echo 
$list[$i];

Untested but it should work.
__________________
Always sanitise your data


Best regards
Reply With Quote
  #3 (permalink)  
Old 01-03-09, 05:09 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by landing View Post
Do you mean you have a string separated with commas in the format number,number,number ?

If so, then the following is for you

PHP Code:

$string "99,0100,1.2345";
$list explode(','$string);

for (
$i 0$i count($list); $i++) {
     echo 
$list[$i];

Untested but it should work.
Ah oh, thanks for your help but in my first post I have caused misunderstanding. I was going to ask for how to return all numbers such as : -99, 0100, 1.2345 ... ( integer, negative numbers, dotted ... ) in a string but I can't edit my post.
Reply With Quote
  #4 (permalink)  
Old 01-03-09, 08:08 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
That code should be fine for that too. If you have an organised string where the values are all separated with a common delimiter (in this case a comma) then the explode() function can split that up for you.

If your string may contain things other than numbers, it might be worth running an is_numeric() check on each part for validation.

Revised (and still untested) code

PHP Code:

$string "99,0100,1.2345";

$list explode(', '$string);

for (
$i 0$i count($list); $i++) {
     if (
is_numeric($list[$i])) {
          echo 
$list[$i]; // Will print only numbers from the list
     
}

__________________
Always sanitise your data


Best regards
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP C-Sharp DB Access Wrapper Question digioz ASP.NET 1 09-08-08 09:54 AM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 09:50 AM


All times are GMT -5. The time now is 08:26 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.