0

I don't want to allow a multiple patterns in a string, such as,

$string = "php in  stackoverflow"; // multiple spaces not allowed
$string = "php in--stackoverflow"; // multiple hyphens not allowed
$string = "php in__stackoverflow"; // multiple underscores not allowed
etc

So, it works with these lines,

if(preg_match('/\s\s+/', $string)) echo "only a single spaces are allowed";
if(preg_match('/\-\-+/', $string)) echo "only a single hyphens are allowed";
if(preg_match('/\_\_+/', $string)) echo "only a single underscores are allowed";

I also don't want to allow the combination patterns below and the lines above don't work,

$string = "php in -stackoverflow"; // a space with a hyphen not allowed
$string = "php in-_stackoverflow"; // a hyphens with a underscore not allowed
$string = "php in_ stackoverflow"; // a underscore with a space not allowed
etc

Any idea I can achieve this with a simple script?

1
  • Do you want to check for two of any of these characters in a row? Commented Nov 1, 2012 at 17:48

1 Answer 1

2

This is what character classes are for, matching multiple characters.

if(preg_match('/[\s-_][\s-_]+/', $string)) echo "only a single space, hyphen or underscore is allowed";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.