PHP String or Character Replacement (preg_replace)

Sometimes you want to run multiple replacements on a string of text or an array. This method uses matching arrays for patterns and replacements to make it easy to see what is being swapped for what.

<?php
$aPatterns
= array(
               
0 => '/"/', // look for this
    
);
$aReplacements = array(
               
0 => '', // replace with this
   
);
           
 
$sSafeName = preg_replace($aPatterns, $aReplacements, $sStringOfTextNeedingCleaning);  //could also use $aArrayOfTextNeedingCleaning
?>

Here is an example of what you get

<?php
$aPatterns
= array(
               
0 => '/"/', // look for this
               
1 => '/help/',
               
2 => '/now/',
              
=> '/!/',
     );
$aReplacements = array(
               
0 => '', // replace with this
               
1 => 'money',
               
2 => 'now',
              
=> '.',
    );

$sStringOfTextNeedingCleaning = 'I need "some" help now!';
           
 
$sSafeName = preg_replace($aPatterns, $aReplacements, $sStringOfTextNeedingCleaning);  //could also use $aArrayOfTextNeedingCleaning

echo $sStringOfTextNeedingCleaning;
echo
$sSafeName;

//would output
// I need "some" help now!
// I need some money please.
?>

Manual description for preg_replace http://php.net/manual/en/function.preg-replace.php

section: