preg_replace 2 strings same code same function

Discussion in 'PHP' started by pixmania, Jul 20, 2009.

  1. #1
    I'm trying to replace 2 parts of a javascript code

    line1
    line2

    $function preg_replace(find, replace, '');
    echo $function;

    How do I write the same code to replace line 2, which will look for a different item and replace it with something different
     
    pixmania, Jul 20, 2009 IP
  2. Webmaster Perks

    Webmaster Perks Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You probably don't even need preg_replace(). You should use str_replace() if you know exactly what you are looking as it will be faster.


    
    $strJavaScript = <<<EOF
    // Insert a whole bunch
    // of JavaScript here
    EOF;
    
    $strSearch1 = '** Edit: Script code to find **';
    $strSearch2 = '** Edit: More script code to find **';
    
    $strReplace1 = '** Edit: New value for $Search1 **';
    $strReplace2 = '** Edit: New value for $Search2 **';
    
    print(str_replace(
      array($strSearch1, $strSearch2), 
      array($strReplace1, $strReplace2), 
      $strJavaScript);
    
    Code (markup):
    If you actually need regular expression pattern matching then do this:

    
    $strJavaScript = <<<EOF
    // Insert a whole bunch
    // of JavaScript here
    EOF;
    
    $strSearch1 = '** Edit: Script to find **';
    $strSearch2 = '** Edit: More sript to find **';
    
    $strReplace1 = '** Edit: New value for $Search1 **';
    $strReplace2 = '** Edit: New value for $Search2 **';
    
    print(ereg_replace(
      $strSearch1, $strReplace1, ereg_replace(
        $strSearch2, $strReplace2, $strJavaScript));
    
    Code (markup):
     
    Webmaster Perks, Jul 21, 2009 IP