Jump to content

Recommended Posts

I want to accept only letters, periods, single quote and spaces.

If I take out the \s, the expression works fine, but once I add the \s for the space in, it never passes. Any Ideas on what I should do?

 

This is what I have:

if( !eregi("^[A-Za-z.'\s]+$", $city) ) {
echo "<BR><FONT COLOR=RED><B>Invalid city</B></FONT>";
return false;
}

 

Link to comment
https://forums.phpfreaks.com/topic/119058-solved-eregi-help/
Share on other sites

I want to accept only letters, periods, single quote and spaces.

If I take out the \s, the expression works fine, but once I add the \s for the space in, it never passes. Any Ideas on what I should do?

 

This is what I have:

if( !eregi("^[A-Za-z.'\s]+$", $city) ) {
echo "<BR><FONT COLOR=RED><B>Invalid city</B></FONT>";
return false;
}

 

For starters, I would avoid using ereg and start using preg (which is part of PCRE - or Perl Compatible Regular Expressions). As of PHP 6, support for POSIX (which uses the ereg syntax) will be dropped. PCRE is more robust and powerful.

 

Here is what you were trying to do , but using preg:

$city = 'New York';
if(preg_match('#^[a-zA-Z\s\.\']+$#', $city)){
   echo $city . ' meets the criteria!';
} else {
   echo $city . ' does not meet the criteria!';							
}

 

Notice how I used the escape character for the dot (which by itself without escape character is basically a wildcard that can be any character [execpt for \r, \n or \t if I am not mistaken]). Since I am using single quotes, I have to escape the single quote inside the delimiters so that the system knows I am actually referring to a literal single quote as opposed to confusing the system with a single quote (which closes the pattern which PCRE matches against).

 

So with or without a space, this work.

 

Cheers,

 

NRG

 

Link to comment
https://forums.phpfreaks.com/topic/119058-solved-eregi-help/#findComment-613194
Share on other sites

Notice how I used the escape character for the dot (which by itself without escape character is basically a wildcard that can be any character [execpt for \r, \n or \t if I am not mistaken]).

 

Metacharacters change within character classes--[.] will only match a period.

 

I want to accept only letters, periods, single quote and spaces.

 

If you only want spaces, be wary of \s. It encompasses all whitespace, thus allowing tabs, new lines, etc.

 

Link to comment
https://forums.phpfreaks.com/topic/119058-solved-eregi-help/#findComment-613562
Share on other sites

Notice how I used the escape character for the dot (which by itself without escape character is basically a wildcard that can be any character [execpt for \r, \n or \t if I am not mistaken]).

 

Metacharacters change within character classes--[.] will only match a period.

 

Oops.. I stand corrected (I'm so used to needing to escape the dot). My bad  ;)

 

Cheers,

 

NRG

Link to comment
https://forums.phpfreaks.com/topic/119058-solved-eregi-help/#findComment-613699
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.