WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > IT Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

IT Discussion Forum Having IT issues? Got IT questions? Who doesn't? If you can't get your Apache to work with your MySQL or your php is choking on your ODBC... Let's see if we can help you come up with some ideas.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-28-2008, 09:56 AM
kruser kruser is offline
WebProWorld Pro
 

Join Date: Aug 2007
Location: Southern Illinois USA
Posts: 141
kruser RepRank 0
Default PHP allow only numbers in string

Hello,

I am looking for a way to only allow the numbers 0-9 in a string. I would like to remove any other characters.

Example: $string="$1,234 Tom";
would like to script to make $string="1234";

I once saw someone do this as a sucurity measure, but can't find it.
__________________
Randy
Rocking Chairs
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:19 AM
Alan-Hart's Avatar
Alan-Hart Alan-Hart is offline
WebProWorld Member
 

Join Date: Jun 2007
Location: Swindon
Posts: 59
Alan-Hart RepRank 0
Default Re: PHP allow only numbers in string

I think this should do it:

$string = ereg_replace("[^0-9]", "", $string);
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:20 AM
Alan-Hart's Avatar
Alan-Hart Alan-Hart is offline
WebProWorld Member
 

Join Date: Jun 2007
Location: Swindon
Posts: 59
Alan-Hart RepRank 0
Default Re: PHP allow only numbers in string

preg_replace(), which uses a Perl compatible regular expression syntax, is a faster alternative to ereg_replace()
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:31 AM
kruser kruser is offline
WebProWorld Pro
 

Join Date: Aug 2007
Location: Southern Illinois USA
Posts: 141
kruser RepRank 0
Default Re: PHP allow only numbers in string

Thanks Alan,

Both functions worked for me.
Also thanks for the tip on the faster one.
__________________
Randy
Rocking Chairs
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 10:34 AM
Alan-Hart's Avatar
Alan-Hart Alan-Hart is offline
WebProWorld Member
 

Join Date: Jun 2007
Location: Swindon
Posts: 59
Alan-Hart RepRank 0
Default Re: PHP allow only numbers in string

No worries glad to help
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > IT Discussion Forum


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I get a variable name as a string? montyauto Web Programming Discussion Forum 12 01-18-2007 05:20 PM
® Registered Mark in URL String? incrediblehelp Search Engine Optimization Forum 3 03-16-2006 06:11 PM
Inserting ASCII into your SQL string. ? fastedge Web Programming Discussion Forum 3 02-21-2006 09:49 PM
How Long is a piece of string ?? expo Search Engine Optimization Forum 11 02-19-2005 07:48 PM
Just read the SEO is BS string,.. mogrady Search Engine Optimization Forum 3 11-23-2004 04:31 PM


Search Engine Friendly URLs by vBSEO 3.0.0