PHP str_replace with wild card?

前端 未结 1 741
渐次进展
渐次进展 2020-12-20 17:50

I know how to use str_replace for simple tasks, like this...

$Header = str_replace(\'

Animals

\', \'

Animals

\', $Heade
相关标签:
1条回答
  • 2020-12-20 18:21

    You can use the str_replace() technique that has been suggested in the primary comments, but I believe preg_replace() using regular expressions is the best practice for wildcard replacements.

    <?php
    $str = <<<EOD
    
    <h3>Animals</h3>
    <h3>Plants</h3>
    
    EOD;
    
    $str = preg_replace('/<h3>(.*?)<\/h3>/', '<h3><span class="One">$1</span></h3>', $str);
    
    echo $str;
    
    0 讨论(0)
提交回复
热议问题