I would like to find a pattern {text}
and replace text including curly braces.
$data = 'you will have a {text and text} in such a format to do {code and code}';
$data= preg_replace_callback('/(?<={{)[^}]*(?=}})/', array($this, 'special_functions'),$data);
and my special function
contain the callback code to replace the braces and completely and text conditionally.
public function special_functions($occurances){
$replace_html = '';
if($occurances){
switch ($occurances[0]) {
case 'text and text':
$replace_html = 'NOTEPAD';
break;
case 'code and code':
$replace_html = 'PHP';
break;
default:
$replace_html ='';
break;
}
}
return $replace_html;
}
Expected Output
you will have a NOTEPAD in such a format to do PHP
How can i replace text and curly braces at the same time using preg_replace_callback
in php using regex