Can you change a specific word in a series of PHP files BULK/MASS?

Discussion in 'PHP' started by mycompanyname, Jan 3, 2012.

  1. #1
    I have a folder with about 1000 PHP files in it, each file contains a domain name (for instance, example.com) Is there a way to run some type of automated script or something to open and change all the "example.com" to "newdomain.com"? Or, would I have to go file by file. I don't have anything backed up in a database, just the files from the FTP. Please let me know if you have any ideas. Thanks.
     
    mycompanyname, Jan 3, 2012 IP
  2. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #2
    I don't see why are those URL's in separate (more than 1000) PHP files but I think you should use preg_replace to change your url's, something like :
    
    $url_to_change = 'example.com';
    $pattern = '/newdomain.com/';
    $replacement = 'example.com';
    $new_url = preg_replace($pattern, $replacement, $url_to_change);
    
    PHP:
    I would store the urls in a database and then run the script above in a while loop :
    
    $url_to_change = $database_row['urls'];
    $patterns = array();
    $patterns[1] = '/example1.com/';
    $patterns[2] = '/example2.com/';
    $patterns[3] = '/example3.com/';
    $replacements = array();
    $replacements[1] = 'newdomain1.com';
    $replacements[2] = 'newdomain2.com';
    $replacements[3] = 'newdomain3.com';
    echo preg_replace($patterns, $replacements, $url_to_change);
    
    PHP:
    I hope you got the idea. :)
     
    dujmovicv, Jan 4, 2012 IP
  3. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #3
    You could also just download Notepad++ and use its seach-replace function. You can "search in data" where you can specify the directory where your 1000 files are, search for "example.com" and replace with "newdomain.com. You can even use regular expressions if needed.
     
    GMF, Jan 4, 2012 IP
  4. AlexanderZ

    AlexanderZ Member

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    48
    #4
    +1 to GMF. Also, PSPad and Eclipse also have this functionality. If your editor does not have similar functionality, I suggest you upgrade. In the future, it is a good idea to either store that domain in a variable, or if it is the current host, to use $_SERVER['HTTP_HOST']
     
    AlexanderZ, Jan 4, 2012 IP