Anil D
Anil D

Reputation: 2009

PHP Regex to allow special characters but no any space

I have the following Regex to allow alphanumeric characters and following special characters

/()-

The Regular expression is

/[^A-Za-z0-9-()-/]/

The complete method is

public function ValidateNumber($number)
{
    $return = true;
    $matches = null;
    if((preg_match('/[^A-Za-z0-9-/()-]/', $number, $matches)) > 0)
    {
       $return = false; 
    }
    return $return;
}

Above method woks fine, but also return TRUE if number has space. When i remove '/' from Regex then if number has 'space' in it then it returns FALSE.

So seems some issue with '/' in Regex.

Please advise some solution

Upvotes: 0

Views: 6176

Answers (4)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

You should escape / in your regex using \/

But you should probably use the following expression to do what you want:

([^A-Za-z0-9-()-\/])+

So the whole method could look like this:

public function ValidateNumber($number)
{
    if (preg_match('/([^A-Za-z0-9-()-\/])+/', $number)) {
       return false;
    }
    return true;
}

without extra variables.

In above case you try to find any characters that don't match (here ^ means characters that don't match) your criteria and if any of them is found preg_match return 1 so it means that number is invalid.

However you can also use another expression to achieve what you want - you don't find characters that don't match (as in previous example) but you check if the whole string matches your criteria using ^ as the beginning (in this case it means the beginning of the string - meaning is different that the one in previous solution) and $ as the end of the string to check the whole string. In this case your method could look like this:

public function ValidateNumber($number)
{
    if (preg_match('/^([A-Za-z0-9-()-\/]+)$/', $number)) {
       return true;
    }
    return false;
}

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

Regex to allow alphanumeric characters and the the above mentioned special characters /()-,

^[A-Za-z0-9()\/-]+$

^ inside(at the strat of) chracter class means not. So your regex allows any character not of the ones mentioned inside the character class. And also it's better to escape / inside the character class and always consider in putting - at the start or end of the character class. To allow one ore more characters which was mentioned inside char class then you need to add + after the character class.

Explanation:

^                        the beginning of the string
[A-Za-z0-9()\/-]+        any character of: 'A' to 'Z', 'a' to 'z',
                         '0' to '9', '(', ')', '\/', '-' (1 or more
                         times)
$                        before an optional \n, and the end of the
                         string

Upvotes: 1

Veerendra
Veerendra

Reputation: 2622

For Much better understanding and learning regex for the further work you can visit the below links

Learning Regular Expressions

Useful regular expression tutorial

Regular expressions tutorials

And one of the best and easy one and my favourite is

http://www.9lessons.info/2013/10/understanding-regular-expression.html?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+9lesson+%289lessons%29

very nice and easy tutorial for the beginners

Upvotes: -1

zx81
zx81

Reputation: 41838

Use this:

$theregex = '~^[a-z0-9/()-]+$~i';
if (preg_match($theregex, $yourstring)) {
    // Yes! It matches!
    } 
else { // nah, no luck...
     }

Explanation

  • The i flag at the end makes it case-insensitive
  • The ^ anchor asserts that we are at the beginning of the string
  • To match a hyphen in a [character class], place it at the beginning or at the end so that it is not ambiguous, since it may indicate a range, as in a-d
  • [a-z0-9/()-]+ matches one or more letter, digit, slash, parenthesis or hyphen
  • The $ anchor asserts that we are at the end of the string

Upvotes: 4

Related Questions