Re: need a find/replace command to fix my require_once

From: Date: Fri, 20 Nov 2009 09:20:31 +0000
Subject: Re: need a find/replace command to fix my require_once
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-299952@lists.php.net to get a copy of this message
On Thu, 2009-11-19 at 17:43 -0800, Daevid Vincent wrote: > I was reading this: > http://pear.php.net/manual/en/standards.including.php > and it states: > > "Note: include_once and require_once are statements, not functions. > Parentheses should not surround the subject filename." > > I never knew that. I've always (wrongly) used: > > require_once('/path/to/my/file'); > > Anyone have a bash command line snippet (or other code is fine too I guess) > that will fix all my directory tree? > > The tricks are that I think there can be several variations and several > instances with a given file too: > > require_once('/path/to/my/file'); > require_once ('/path/to/my/file'); > require_once("/path/to/my/file"); > require_once(ROOTPATH."/my/file"); > > etc. Note the space before the parens, the single vs. double quotes and the > use of a global define. > > I think this regex should work: > > require_once\s?\((.*?)\); > > I just don't know the magic sed/awk/grep/whatever to do the search and > replace on all my .php files recursively. > > This seems like a useful 'routine' to post up on that page, as I'm sure I'm > not the only one who didn't know this little subtlety (considering PHP > doesn't puke on it either). > > You could try something like this: find . -maxdepth 1 -name "*.php" -type f -exec sed -i 's/find text/replace text/' {} \; Note that the find and replace strings are regular expressions, separated by the / character here, so ()'s will need to be escaped as they have a special meaning in regular expressions. The maxdepth parameter of find can be set to higher than 1 to let the script work recursively on files within directories. Thanks, Ash http://www.ashleysheridan.co.uk

« previous php.general (#299952) next »