hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP: String validation
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

PHP: String validation

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-02-2005, 01:55 AM
mylinear mylinear is offline
Junior Guru Wannabe
 
Join Date: Jul 2005
Posts: 77

PHP: String validation


How do I validate whether a string meets the following criteria.
1. Must be 6 to 12 characters in length
2. Must consists of only a to z with no spaces etc

I was using:
$string = "abcdef";
$pattern = "/[a-z]{6,12}/";
preg_match($pattern, $sring);
// returns true

But I found that it would also return true with other strings such as:
$string = " abcdef xyz";

Do I need to include the 3rd array parameter in preg_match and use the resulting array element to do further checking on the match? Or should I not be using preg_match to do this sort of validation?

Reply With Quote


Sponsored Links
  #2  
Old 12-02-2005, 02:51 AM
mylinear mylinear is offline
Junior Guru Wannabe
 
Join Date: Jul 2005
Posts: 77
Upon further testing, this appears to work.

$pattern = "/^[a-z]{6,12}$/";

But I'm still not sure whether it is the correct way to do this.

Reply With Quote
  #3  
Old 12-02-2005, 08:37 AM
ChristsLittleFlock ChristsLittleFlock is offline
Web Hosting Master
 
Join Date: Jan 2005
Location: Johnstown, Pennsylvania
Posts: 1,269
Yes, you need to include the ^ and $; otherwise, as long as 6 consecutive letters in the string match the pattern, it's happy, even if other characters do not match. The ^ tells it, though, that the pattern must be first in the string, and $ tells it that the pattern must also be last.

__________________
Lamb Host | Shared, reseller, VPS, and dedicated hosting • Pure SSD Hosting • Build your own package • CloudLinux/LiteSpeed • Certified partner of CloudFlare • 24x7 support
Blazing Fast SSD VPSs | Pure SSD storage • 1 Gbps uplink • 99.9% uptime guarantee • 24x7 support • management available

Reply With Quote
Sponsored Links
  #4  
Old 12-03-2005, 08:02 AM
mylinear mylinear is offline
Junior Guru Wannabe
 
Join Date: Jul 2005
Posts: 77
Thanks to Hostultrix-Brandon for the pointer.

I came across some tutorials and examples on the following site that others may find helpful.

http://www.regularexpressions.info/

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
10gen Launches Partner Program to Accelerate Demand for MongoDB Web Hosting News 2013-03-12 14:28:42
50a15988-2160-47c5-99b2-73d145a799bf Listing 2013-03-05 18:23:10
DigiCert to Issue High-Assurance SSL Certificates in Under an Hour Web Hosting News 2012-08-28 17:12:17
DigiCert Launches Extended Validation Code Signing Certificates Web Hosting News 2012-08-15 15:30:28
GlobalSign Integrates OneClickSSL Into WHMCS Billing Platform Web Hosting News 2011-09-26 19:59:58


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?