0

I am facing a little problem. My string is From {start} to {end} and I want to validate it using preg_match() to avoid illegal chars.

preg_match("/^[a-zA-Z\{\} ]{1,}$/",$var)

The point is the escape for { and }.

thanks :)

1 Answer 1

1

You don't need to escape curly braces inside the square brackets:

preg_match('!^[a-zA-Z{} ]+$!', $var);

If you need to escape characters, just put a backslash (\) before them. Note: if you're using a double quoted string, you may need to escape the backslashes from interpretation there. For example:

  • '\t' matches a tab character;
  • "\t" is a tab character; and
  • "\\t" is equivalent to '\t'.
Sign up to request clarification or add additional context in comments.

1 Comment

mmm, ok i actually understand the problem is that string is passed by get.

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.