SyntaxStudy
Sign Up
PHP PHP Regex Modifiers
PHP Intermediate 5 min read

PHP Regex Modifiers

PCRE Modifiers

Modifiers are appended after the closing delimiter. Common ones: i (case-insensitive), m (multiline), s (dot matches newline), x (extended/verbose), u (UTF-8).

Example
<?php
// i: case-insensitive
preg_match("/hello/i", "Hello World"); // matches

// m: ^ and $ match line boundaries
preg_match_all("/^\w+/m", "line1
line2
line3", $m);
// $m[0] = ["line1", "line2", "line3"]

// s: dot matches newline
preg_match("/start.*end/s", "start
middle
end");

// x: extended/verbose mode — whitespace and # comments ignored
$pattern = "/
  ^              # Start of string
  (\d{4})       # 4-digit year
  -              # Separator
  (\d{2})       # 2-digit month
  -              # Separator
  (\d{2})       # 2-digit day
  $              # End of string
/x";
Pro Tip

The /x modifier makes complex patterns readable by allowing whitespace and comments inside the regex literal.