2016-04-01 6 views
0

内の特定の試合を無視し、以下のPHPの文字列を見つけてください:私は[OK]を、私はそれを削除することができます]と[OK]を、私はあまりにもそれを削除することができます] remvoeしたいが、私は[キャプションを保持したいはにpreg_replace

$string = 'hi this is testing [ok i can remove it] and 
then [ok i can remove it too] and then I want to spare [caption .... ]'; 

を.. ..] preg_replaceを使用して文字列内に挿入します。私は親切に案内[]

$return = preg_replace(array('~\[.*]~'), '', $b); 

ですべてを削除して、以下を使用してい 現在。仕事のこの種の

答えて

0

、私はこのようなpreg_replace_callback使用したい:

$string = 'hi this is testing [ok i can remove it] and then [ok i can remove it too] and then I want to spare [caption .... ]'; 
$return = preg_replace_callback(
      '~\[[^\]]*\]~', 
      function($m) { 
       if (preg_match('/\bcaption\b/', $m[0])) 
        return $m[0]; 
       else 
        return ''; 
      }, 
      $string); 
echo $return,"\n"; 

出力:

hi this is testing and then and then I want to spare [caption .... ]