InfoHeap
Tech
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 OR (alternation) examples using pipe

By admin on Jan 22, 2016

Regex OR (alternation) in php can be matched using pipe (|) match character. This is very frequently used in regular expressions. Following are some ways pipe can be used in php regex

  1. (hello|foo|bar) will match any of hello, foo, bar
  2. (|foo|bar) will match foo or bar or nothing. It is equivalent to optional match (foo|bar)?
  3. (^|foo) will match either foo or beginning of string.
  4. (foo|$) will match either foo or end of string.

preg_match example for pattern (A|B|C)

PHP preg_match example which matches with hello or foo or bar with space in beginning and end (ignoring case)

<?php
if (preg_match('/ (hello|foo|bar) /i', "hello world. food bar and something", $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:
 bar  (at offset 17)
Env: PHP version 7.4.33 (Linux)

preg_replace example for pattern (^|A)

PHP preg_replace example which removes hello when preceded by space or is in beginning (ignoring case)

<?php
$str = "hello world. sayhello. hello world again";
$newstr = preg_replace('/(^| )hello/i', "", $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 2 replacement(s):
 world. sayhello. world again
Env: PHP version 7.4.33 (Linux)
Note that hello in sayhello is not replaced here.

preg_replace example for pattern (A|$)

PHP preg_replace example which removes hello when followed by space or is in end (ignoring case)

<?php
$str = "hello world. hellos last hello";
$newstr = preg_replace('/hello( |$)/i', "", $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 2 replacement(s):
world. hellos last 
Env: PHP version 7.4.33 (Linux)
Note that hello in hellos is not replaced here.

Few points to note

  1. It is better to use brackets when using regex OR.
  2. Pipe can be escaped using backslash for its literal use (\|).

Suggested posts:

  1. Python selenium – execute javascript code
  2. Css source map example using gulp-sourcemaps
  3. Bash – local and global variables
  4. PHP regex – match any digit
  5. PHP regex – whitespace shorthand (\s) regex examples
  6. CSS max-height – limit maximum height of an element
  7. Css :first-of-type selector – first child element of type
  8. Javascript/jQuery – disable right click
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, Regular Expression, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | 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 | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress