PHP preg_replace three times with three different patterns? right or wrong?

后端 未结 5 1775
北海茫月
北海茫月 2020-12-30 07:43

A simple question: Is this the best way to do it?

$pattern1 = \"regexp1\";
$pattern2 = \"regexp2\";
$pattern3 = \"regexp3\";

$content = preg_replace($patter         


        
相关标签:
5条回答
  • 2020-12-30 08:10

    I am using it for XML tags string.

    <?php 
    
     $string = '<JUDGE_NAME><NAME>Ahmad Maarop</NAME>CJ, <NAME>Zaharah Ibrahim</NAME>, <NAME>Prasad Sandosham</NAME>, <NAME>Jeffrey Tan</NAME>, <NAME>Alizatul Khair</NAME> FCJJ</JUDGE_NAME>\r\n';
     $patterns = array('/\bCJ\b/', '/\bJCA\b/','/\bJJCA\b/','/\bPCA\b/', '/\bCJM\b/', '/\bCJSS\b/', '/\bFCJ\b/', '/\bFCJJ\b/', '/\bJ\b/','/\bH\b/', '/\bPK\b/','/\bJC\b/', '/\bHMP\b/', '/\bHHMP\b/', '/\bHMR\b/', '/\bHHMR\b/');
     $replacements = array('<rank>CJ</rank>', '<rank>JCA</rank>','<rank>JJCA</rank>','<rank>PCA</rank>', '<rank>CJM</rank>', '<rank>CJSS</rank>', '<rank>FCJ</rank>', '<rank>FCJJ</rank>', '<rank>J</rank>','<rank>H</rank>', '<rank>PK</rank>','<rank>JC</rank>', '<rank>HMP</rank>', '<rank>HHMP</rank>', '<rank>HMR</rank>', '<rank>HHMR</rank>');
    
       
    
    
      $string = preg_replace($patterns,$replacements,$xmlString);
      
      
      echo $string;
    // <JUDGE_NAME><NAME>Ahmad Maarop</NAME> <rank>CJ</rank>, <NAME>Zaharah Ibrahim</NAME>, <NAME>Prasad Sandosham</NAME>, <NAME>Jeffrey Tan</NAME>, <NAME>Alizatul Khair</NAME> <rank>FCJJ</rank></JUDGE_NAME>\r\n ◀
    
      
    ?>

    0 讨论(0)
  • 2020-12-30 08:12

    hope this example helping you to understand "find in array", and "replace from array"

    $pattern=array('1','2','3');
    $replace=array('one','two','tree');
    $content = preg_replace($pattern,$replace, $content);
    
    0 讨论(0)
  • 2020-12-30 08:17

    As you are replacing all with the same, you could do either pass an array

    $content = preg_replace(array($pattern1,$pattern2, $pattern3), '', $content);
    

    or create one expression:

    $content = preg_replace('/regexp1|regexp2|regexp3/', '', $content);
    

    If the "expressions" are actually pure character strings, use str_replace instead.

    0 讨论(0)
  • 2020-12-30 08:23

    A very readable approach is to make an array with patterns and replacements, and then use array_keys and array_values in the preg_replace

    $replace = [
       "1" => "one",
       "2" => "two",
       "3" => "three"
    ];
    $content = preg_replace( array_keys( $replace ), array_values( $replace ), $content );
    

    This even works with more complex patterns. The following code will replace 1, 2 and 3, and it will remove double spaces.

    $replace = [
       "1"       => "one",
       "2"       => "two",
       "3"       => "three",
       "/ {2,}/" => " "
    ];
    $content = preg_replace( array_keys( $replace ), array_values( $replace ), $content );
    
    0 讨论(0)
  • 2020-12-30 08:24

    To do multiple searches in a single preg_replace() call, use an array of patterns. You can still pass a single replacement, this is what's matched by all three patterns is replaced with:

    $content = preg_replace(array($pattern1, $pattern2, $pattern3), '', $content);
    
    0 讨论(0)
提交回复
热议问题