1. Home
  2. Docs
  3. PHP
  4. 字串
  5. preg_replace 字串替換

preg_replace 字串替換

    詳細對照表請參考
    Regular Expressions 正則表達式
    單一替換

    $str = 'Visit Microsoft!';
    $pattern = '/microsoft/i';
    echo preg_replace($pattern, 'powenko', $str);
    

    輸出:
    Visit powenko!


    多部分替換

    $string = 'The quick brown fox jumps over the lazy dog.';
    $patterns = array();
    $patterns[0] = '/quick/';
    $patterns[1] = '/brown/'; 
    $replacements = array('bear','black'); 
    echo preg_replace($patterns, $replacements, $string);
    

    輸出:
    The bear black fox jumps over the lazy dog.


    日期格式替換

    $patterns = array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/',
                       '/^\s*{(\w+)}\s*=/');
    $replace = array ('\3/\4/\1\2', '$\1 =');
    echo preg_replace($patterns, $replace, '{startDate} = 1999-5-27');
    

    輸出:
    $startDate = 5/27/1999


    去除空格
    此示例從字符串中去除多餘的空格。

    $str = 'a      b    c    d';
    echo "[".$str."]<br>"; 
    $str = preg_replace('/\s\s+/', ' ', $str); 
    echo "[".$str."]<br>"; 
    

    輸出:
    [a b c d]
    [a-b-c-d]


    $count = 0;
    echo preg_replace(array('/\d/', '/\s/'), '*', 'xp 4 to', -1 , $count);
    echo "<br>"; 
    echo $count; //3
    

    輸出:
    xp***to
    3


    數字替換

       $copy_date = "Copyright 1999";
       $copy_date = preg_replace("([0-9]+)", "2000", $copy_date);
       print $copy_date;
    

    輸出:
    Copyright 2000


    小寫英文替換

     
       $copy_date = "Copyright 1999";
       $copy_date = preg_replace("([a-z]+)", "powenko", $copy_date);
       
       print $copy_date;
    

    輸出:
    Cpowenko 1999


    大小寫英文替換

     
    
       $copy_date = "Copyright 1999";
       $copy_date = preg_replace("([A-Z,a-z]+)", "powenko", $copy_date);
       print $copy_date;
    

    輸出:
    powenko 1999


    大小寫英文和數字替換

     
       $copy_date = "Copyright 1999";
       $copy_date = preg_replace("([A-Z,a-z,0-9]+)", "powenko", $copy_date);
       print $copy_date;
    

    輸出:
    powenko powenko


     
    $string = 'November 01, 2018';
    $pattern = '/(\w+) (\d+), (\d+)/i';
    $replacement = '$3年 ${1}月 $2日';
       
    echo preg_replace($pattern, $replacement, $string);
    

    輸出:
    2018年 November月 01日


     
    
    $subject = array('1', 'GFG', '2',
    'Geeks', '3', 'GCET', 'Contribute', '4'); 
    $pattern = array('/\d/', '/[a-z]/', '/[1a]/'); 
    $replace = array('X:$0', 'Y:$0', 'Z:$0'); 
      
    // Print Result return by function
    echo "preg_replace returns\n";
    print_r(preg_replace($pattern, $replace, $subject)); 
    

    輸出:
    preg_replace returns
    Array
    (
    [0] => X:Z:1
    [1] => GFG
    [2] => X:2
    [3] => GY:eY:eY:kY:s
    [4] => X:3
    [5] => GCET
    [6] => CY:oY:nY:tY:rY:iY:bY:uY:tY:e
    [7] => X:4
    )


     
    $count = 0;
      
    // Display result after replace and count 
    
    echo preg_replace(array('/\d/', '/\s/'),
            '*', 'Geeks 4 Geeks 5', 1, $count);
    echo "<br>" . $count . "<br>" ;
    echo preg_replace(array('/\d/', '/\s/'),
            '*', 'Geeks 4 Geeks 6', 2, $count);
    echo "<br>" . $count . "<br>" ;
            
    echo preg_replace(array('/\d/', '/\s/'),
            '*', 'Geeks 4 Geeks 6', -1, $count);
    echo "<br>" . $count . "<br>" ;
    

    輸出:
    Geeks** Geeks 5
    2
    Geeks***Geeks *
    4
    Geeks***Geeks**
    5


    找出id 並替換

     
    $string ='
      <div id="myid">this has to be replaced</div>
      <p>here is something</p>
      <div id="any">any text not to be replaced</div>';
    
    echo $string = preg_replace('/<div id=\"myid\">.*?<\/div>/','anything',$string);
    

    輸出:
    anything
    here is something

    any text not to be replaced


    找出id 並替換 (內容有跳行)

     
    $string ='
      <div id="myid">this has to be replaced</div>
      <div id="printhead"><p>ELECTRONICS 18-06-RBO-049-01	EMC TESTING DEPARTMENT </p>
      </div>
    ';
    
    echo $string = preg_replace('/<div id=\"printhead\">.*?<\/div>/s','anything',$string);
    
    

    輸出:
    this has to be replaced
    anything

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出:

     
    

    輸出: