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 – preg_replace back reference (in replacement text) examples

By admin on Jan 20, 2016

Sometimes we need to use back reference in PHP preg_replace to put back the matched pattern. One example can be if you want to append certain string after the pattern. Here are few examples.

preg_replace back reference using back slash

Replace hello with hello-2 (when followed by world) and preserve case. We’ll use \1 to back reference first parenthesis match in replacement text.

<?php
$str = "hello world. Hello world";
$newstr = preg_replace('/(hello)/i', '\1-2', $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):
hello-2 world. Hello-2 world
Env: PHP version 7.4.33 (Linux)

Note that you can use \0 for the full pattern match.

preg_replace back reference using ${1}

Replace hello with hello-2 (when followed by world) and preserve case. We’ll use ${1} to back reference first parenthesis match in replacement text. This approach works better if ${1} is immediately followed by an alphanumeric character in replacement text.

<?php
$str = "hello world. Hello world";
$newstr = preg_replace('/(hello)/i', '${1}-2', $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):
hello-2 world. Hello-2 world
Env: PHP version 7.4.33 (Linux)

Note that you can use ${0} for the full pattern match.

Suggested posts:

  1. Python selenium – execute javascript code
  2. Bash – how to compare file timestamps
  3. PHP – convert dos newline to unix format
  4. PHP regex – whitespace shorthand (\s) regex examples
  5. Bash – local and global variables
  6. CSS max-height – limit maximum height of an element
  7. Running php eval on code with tags
  8. Css :first-of-type selector – first child element of type
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged PHP, PHP Regex, 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