Last active
April 20, 2021 14:04
-
-
Save minte9/758677086ca51065d7d911e3a6968b49 to your computer and use it in GitHub Desktop.
Php / String / Regex - https://www.minte9.com/php/string-regexp-71
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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