Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find string between and replace with PHP

I want to find and replace the string between the 2 sets of {{}}:

<p>I want to {{find}} this {{string}}.</p>

After I want to remove the {{}} and put the 2 strings in bold.

This is what I have tried:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$string = '<p>I want to {{find}} this {{string}}.</p>';
if(preg_match_all('/{{(.*?)}}/', $string, $matches) == 1) {
    foreach ($matches as $match) {
        echo '<b>'.$match.'</b>';
    }
}

>Solution :

You can do this with preg_replace like this

$string = '<p>I want to {{find}} this {{string}}.</p>';
$replaced_string = preg_replace('/{{(.*?)}}/', '<b>$1</b>', $string);
echo $replaced_string;

Output

I want to find this string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading