By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Space in a preg match function

P: 9
Hi

How do i get space inside my preg match function?

Expand|Select|Wrap|Line Numbers
  1.  
  2. if (!preg_match('/^[A-Za-z]+$/', $facebook))
  3.  
  4.  

Google is no help today
Apr 26 '10 #1

✓ answered by Atli

Hey.

You just add a space. They have no special meaning inside a regular expression, and they are not parsed away like with the PHP code. If you want the regular expression to match a space, you just enter a space.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $text = 'My name is Atli';
  3. $regexp = '/^[a-z ]+$/i';
  4.  
  5. if(preg_match($regexp, $text)) {
  6.     echo "Success!";
  7. }
  8. else {
  9.     echo "Failure!";
  10. }
  11. ?>
This echoes "Success!".

Also note the "i" following the closing delimiter. This makes the match case-insensitive, removing the need to add both upper and lower case letters in the regular expression syntax. Either will match both.

Share this Question
Share on Google+
4 Replies


Atli
Expert 5K+
P: 5,058
Hey.

You just add a space. They have no special meaning inside a regular expression, and they are not parsed away like with the PHP code. If you want the regular expression to match a space, you just enter a space.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $text = 'My name is Atli';
  3. $regexp = '/^[a-z ]+$/i';
  4.  
  5. if(preg_match($regexp, $text)) {
  6.     echo "Success!";
  7. }
  8. else {
  9.     echo "Failure!";
  10. }
  11. ?>
This echoes "Success!".

Also note the "i" following the closing delimiter. This makes the match case-insensitive, removing the need to add both upper and lower case letters in the regular expression syntax. Either will match both.
Apr 26 '10 #2

P: 9
Thanks that work great;)

So if I want the user to be able to write http://www.facebook.dk/joe.m

Should i then write the code like this.

Expand|Select|Wrap|Line Numbers
  1.  
  2. [A-Za-z0-9_-://]+[.]+[A-Za-z]+[.]+[A-Za-z0-9_-://.]
  3.  
  4.  
Apr 27 '10 #3

Dormilich
Expert Mod 5K+
P: 8,639
no need to make it overly complicated:
Expand|Select|Wrap|Line Numbers
  1. defined with the "i" modifyer
  2. http://(?:[-\w]+\.){1,4}[a-z]{2,4}/[\w.]+
  3.  
  4. "http://" - obvious
  5. "(?:)" - a non-capturing group
  6. "[-\w]" - a hyphen or word character ([a-zA-Z0-9_])
  7. "{min,max}" - at least min times and at most max times
PS. afaik, URLs allow only ASCII characters
Apr 27 '10 #4

Atli
Expert 5K+
P: 5,058
PS. afaik, URLs allow only ASCII characters
That's not true anymore.

In 2003 the IDN (Internationalized Domain Name) specification was released, allowing DNS servers to register names using most Unicode characters.

For backwards compatibility, they are still registered as ASCII strings, but client software compatible with IDNs should allow you to use non ASCII URLs and do the ASCII conversion in the background.
For example, the URL http://blai.is/ is registered as http://xn--blai-dqab.is/. Clicking the second one should work in all browsers, but the first one should only work in IDN capable browsers.

P.S.
I have no idea who's website that is. I just picked a random URL with non ASCII chars in it :P
Apr 27 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.