Announcement

Collapse
No announcement yet.

PHP Howto: Do I need to use forward slashes at the start and end of a regular expression?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Howto: Do I need to use forward slashes at the start and end of a regular expression?

    All regular expressions in PHP (i.e. regular expressions using preg_match(), preg_replace() and other PCRE functions) must have delimiters - that is, to specify the start and end of a regular expression. However, you don't need to strictly use the forward slash character. In fact, you can use the hash/pound character ( # ) or perhaps the plus character ( + ) and many others.

    Example:
    Code:
    $regex = preg_match( '|[a-zA-Z]+|',$string);
    Remember, if you need to use the character that is your delimiter within your regular expression, it needs to be preceded with a backslash so PHP ignores it and treats it as a literal character and not as the end of your regular expression.
Working...
X