Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Replacing A Common String Across Multiple Text Files..

  • 02-12-2005 5:44pm
    #1
    Registered Users, Registered Users 2 Posts: 1,280 ✭✭✭


    Hello,

    I am maintaining a PHP based website which I am now converting to a static format for release on CD-ROM. Does anyone know of a utility/method for replacing a common string of text (eg. "http://www.anything.com/) with another string (eg. "" - blank string) across multiple files. That is, without manually having to go into each file and doing an Edit->Replace...

    There's got to be a simple way of doing this and would save a hell of a lot of time (need to replace strings in over 100 HTML files..)

    Any help appreciated,

    jAH


Comments

  • Closed Accounts Posts: 382 ✭✭misterq


    Notepad++ can do a find and replace in all open files so you can open all the files then run it. Best solution I could find (that took no more than 10mins) when trying to do something simlar.

    http://notepad-plus.sourceforge.net/


  • Registered Users, Registered Users 2 Posts: 1,169 ✭✭✭dangerman


    textpad does it too. Just do Search >> Replace and select 'all documents' - obviously with all documents open.

    http://www.textpad.com/


  • Registered Users, Registered Users 2 Posts: 304 ✭✭PhantomBeaker


    One idea might be to use PHP's own libraries for this.

    Basic idea (because I don't know the calls offhand):

    Have an array containing the directory you want to work on. We'll call this the directory array
    Have an array to hold all the files you'll want to open - this will be empty to begin with. We'll call this the file array
    While there are still elements on the directort array:
      Pop a directory off the array (you can use array_pop() for this),
      Open that directory
      List all the files and push (using array_push) them onto your file array if they're files, or onto your directory array if they're a directory.
      while there are still files on your file array:
        pop a file off
        fopen that file
        process it with the replacement commands that are there (see http://www.php.net/manual/en/ref.pcre.php and http://www.php.net/manual/en/ref.regex.php for reference, those are the two that php provides)
        write it either to a new file, or to the same file, whichever you require
      done
    done
    

    So basically you're systematically opening all your directories and subdirectories, and opening each up, and editting the files and storing your directories for later processing, then processing those when you're through your files.

    Plus you learn your way around php a bit more. Also it means you don't need to download all of them via ftp, or have to have shell access. If I have a chance I'll trying knocking it up, and posting it on here, because you're not the first who's wanted to do this.

    Take care,
    P.B.


  • Closed Accounts Posts: 334 ✭✭WhatsGoingOn


    jArgHA wrote:
    Hello,

    I am maintaining a PHP based website which I am now converting to a static format for release on CD-ROM. Does anyone know of a utility/method for replacing a common string of text (eg. "http://www.anything.com/) with another string (eg. "" - blank string) across multiple files. That is, without manually having to go into each file and doing an Edit->Replace...

    There's got to be a simple way of doing this and would save a hell of a lot of time (need to replace strings in over 100 HTML files..)

    Any help appreciated,

    jAH

    Search and Replace is the best tool I use, perfect for what you are looking for http://www.funduc.com/search_replace.htm. It's not free though, think it costs about $20


  • Registered Users, Registered Users 2 Posts: 7,518 ✭✭✭matrim


    editplus also has a search and replace across all open files


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,280 ✭✭✭jArgHA


    Thanks for all the tips

    I use Textpad regularly but didn't realise it was capable of doing this so cheers again for the heads up. Problem solved!

    jAH


  • Registered Users, Registered Users 2 Posts: 4,142 ✭✭✭TempestSabre


    One idea might be to use PHP's own libraries for this.

    Basic idea (because I don't know the calls offhand):

    Have an array containing the directory you want to work on. We'll call this the directory array
    Have an array to hold all the files you'll want to open - this will be empty to begin with. We'll call this the file array
    ....


    Not all problems SHOULD be solved by coding. :cool:


Advertisement