How to use php preg_replace to replace HTML tags

前端 未结 3 1446
执念已碎
执念已碎 2020-12-17 04:11

I\'d like to change

 with  and 
with .

I\'m having problem with t

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 04:27

    I found a very easy solution to replace multiple words in a string :

    Hello world!
    "; $pattern=array(); $pattern[0]="/
    /";
    $pattern[1]="/<\/pre>/";
    
    
    $replacement=array();
    $replacement[0]="";
    $replacement[1]="";
    
    echo preg_replace($pattern,$replacement,$str);?> 
    

    output :

     Hello world!
    

    With this script you can replace as many words in a string as you want :

    just place the word (that you want to replace) in the pattern array , eg :

         $pattern[0]="/replaceme/"; 
    

    and place the characters (that will be used in place of the replaced characters) in the replacement array, eg :

          $replacement[0]="new_word"; 
    

    Happy coding!

提交回复
热议问题