2016-04-01 5 views
0

finden Sie PHP-String unter:Ignorieren bestimmte Begegnungen in 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 .... ]'; 

ich remvoe will [ok ich es entfernen] und [ok ich kann es entfernen], aber ich will [caption halten .. ..] in der Zeichenfolge mit preg_replace. Strom I folgende verwenden, die alle mit []

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

freundlich führen wird zu entfernen.

Antwort

0

Für diese Art von Job, ich preg_replace_callback wie folgt verwenden würde:

$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"; 

Ausgang:

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