Skip to content

Instantly share code, notes, and snippets.

@minte9
Last active April 20, 2021 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minte9/758677086ca51065d7d911e3a6968b49 to your computer and use it in GitHub Desktop.
Save minte9/758677086ca51065d7d911e3a6968b49 to your computer and use it in GitHub Desktop.
<?php
/**
* Replace
* {b} abc {/b} with:
* <b> abc </b>
* Use preg_replace() function
*/
$str = "{b} abc {/b}";
/**
* SOLUTION
*/
$pattern = "/{b} (.*) {\/b}/";
preg_match($pattern, $str, $matches);
print_r($matches); // [1] => "abc"
$replace = preg_replace($pattern, "<b> $1 </b>", $str);
echo $replace; // <b> abc </b>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment