InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PHP regex

  • preg_match online
  • preg_match_all online
  • preg_replace online
  • Regex OR (alternation)
  • beginning and end of string
  • look ahead and look behind
  • preg_match - greedy and lazy
  • preg_replace - optional char group
  • preg_replace - optional one char
  • preg_replace back reference (in replacement text)
  • preg_replace – back reference within pattern
  • preg_replace – dot all
  • regex delimiters
  • regex shorthand - digit (\w)
  • regex shorthand - whitespac (\s)
  • regex shorthand - word boundary (\b)
  • regex shorthand - word character (\w)
  • remove non printable chars
 
  • Home
  • > Tutorials
  • > PHP
  • > PHP Regex

Php regex delimiter examples

By admin on Jan 10, 2016

PHP requires regex patterns to have valid delimiters around it. Some languages may not require delimiters (e.g. python). To make code more readable, sometimes we want to use different delimiters. This article will list some commonly used delimiters.

php-regex-delimiters

List of commonly used php regex delimiters

Any non-alphanumeric, non-backslash, non-whitespace character can be used as pattern delimiter in PCRE regular expressions. Here are some commonly used delimiters:

  1. /foo\/bar/
  2. #/foo/bar/#
  3. %foo/bar%
  4. |foo/bar|
  5. ~foo/bar~
  6. {foo/bar}
  7. [foo/bar]
  8. <foo/bar>
  9. (foo/bar)

Since brackets are frequently used in regular expressions, using other delimiters makes code more readable. I prefer using slash or hash.

Some preg_match and preg_replace regex delimiter examples

preg_match using slash (/) as delimiter

<?php
if (preg_match('/foo\/bar/', "abc/foo/bar/def", $matches, PREG_OFFSET_CAPTURE)) {
  echo "matched string:\n{$matches[0][0]} (at offset {$matches[0][1]})\n";
} else {
  echo "No match\n";
}
?>
try it online
matched string:
foo/bar (at offset 4)
Env: PHP version 7.3.18 (Linux)

preg_match using hash (#) as delimiter

<?php
if (preg_match('#foo/bar#', "abc/foo/bar/def", $matches, PREG_OFFSET_CAPTURE)) {
  echo "matched string:\n{$matches[0][0]} (at offset {$matches[0][1]})\n";
} else {
  echo "No match\n";
}
?>
try it online
matched string:
foo/bar (at offset 4)
Env: PHP version 7.3.18 (Linux)

preg_replace using percent (%) as delimiter

<?php
$str = "abc/foo/bar/def";
$newstr = preg_replace('%foo/bar%', '_foo_/_bar_', $str, -1, $count);
if ($count > 0) {
  echo "newstr after $count replacement(s):\n$newstr\n";
} else {
  echo "No replacement\n";
}
?>
try it online
newstr after 1 replacement(s):
abc/_foo_/_bar_/def
Env: PHP version 7.3.18 (Linux)

Suggested posts:

  1. Php look ahead and look behind regex examples
  2. PHP – Regex OR (alternation) examples using pipe
  3. php preg_match – greedy and lazy regex examples
  4. PHP regex – word boundary examples
  5. PHP beginning and end of string regex examples
  6. PHP regex – match word character (letter, number or underscore)
  7. PHP regex – match any digit
  8. PHP regex – whitespace shorthand (\s) regex examples
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged PHP, PHP Regex, preg_match, preg_replace, Tutorials

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2022 InfoHeap.

Powered by WordPress