Search and Replace PHP split() with Python

A couple of days ago I upgraded to Ubuntu 10.04 which, in turn, upgraded my PHP version from 5.2 to 5.3. In PHP 5.3 they have completely deprecated the use of the split() function in favour of explode(). I did a quick search in my /work directory and it turns out that I have 832 files that are affected by this. All of them need to have split() replaced with explode() or the websites will throw errors every time they encounter it.

Here’s how I did it using an altered Python script I wrote a couple of years ago:

#!  /usr/bin/python
import os
import re

mydir = "/home/damian/work"

def doReplace(filePath):
	fin = open(filePath, "r")
	s = fin.read()
	fin.flush()
	fin.close()
	p = re.compile('(\s|\(|=)split\(')
	if p.search(s):
		fout = open(filePath, "w")
		s = p.sub(r'\1explode(', s)
		fout.write(s)
		fout.close()

for root, dirs, files in os.walk(mydir):
	for f in files:
		name, ext = os.path.splitext(f)
		if ext == '.php':
			doReplace(root + '/' + f)

(**UPDATE** I’ve switched the search and replace to use regular expressions because I found that ‘split()’ can be prefixed by a number of symbols but not others — i.e. don’t replace ‘preg_split()’)

Tags: , , ,

6 Responses to “Search and Replace PHP split() with Python”

  1. ropata says:

    Not another python mega script! It’s like enterprise java :P

    bash is what you need

    #!/bin/sh
    for file in `grep -r -l abc *`
    do sed  's/abc/xyz/g'; $file > $file.fixed
    echo $file.fixed
    done
  2. Damian says:

    That’s certainly far smaller than the Python solution! I’m not very familiar with bash though. How would it look to exactly replicate the solution I wrote? (just looking for .php files and using the same regex).

    I’ll give it a go once I understand what’s happening.

  3. Damian says:

    (oh, and I cleaned up your code a bit by wrapping it in ‘pre’ tags. I notice you must usually use some kind of code rewriter?)

  4. ropata says:

    If I read your code correctly, this should replicate it, assuming that grep and sed handle regex the same as Python. NOT tested, and somewhat more violent than my previous example. (A filename handling bug is fixed though)

    #!/bin/sh
    cd /home/damian/work

    grep -r -l -E ‘(\s|\(|=)split\(‘ * | while read FILE
    do
    sed -i ‘s/(\s|\(|=)split\(/\1explode(/g’ $FILE
    done

    “grep -r -l” : do recursive text search from current directory, list files that have a match

    “-E” : grep option for extended regex, “*” means match any regular file

    “while read FILE ; do .. done” assign the next row of text returned by grep to a variable called $FILE, and iterate

    “sed -i <expression> $file” : in-place edit a file called $FILE with commands issued in <expression>

    The <expression> is a common search and replace. This part is the weakest, since I’m not sure what the regex is doing :/ — would need to test it

    PS: in my other comment I used tags for the SyntaxHighlighter plugin, supported on most wordpress.com blogs.

  5. Damian says:

    Cheers Ropata,
    I had a go with the script you provided but it’s failing and I suspect it is also not only looking for *.php files.

    I have little doubt that bash is the most ninja-est of ninja-ry scripts but that seems to come at the expense of comprehension and, importantly, platform independence ;) and when you are editing multiple files in multiple sub-directories that can be a pretty dangerous combination!

    I use bash from time to time to automate SQL dumps and the like but I do quite like Python for its legibility and share-ability.

  6. ropata says:

    Hi again
    I guess it’s what you’re used to .. I’ve messed round with Unix since 19{ahem-ahem}. It can be multi platform with cygwin, windows SFU, or MingW. I think Perl is more ninja like, but hideous to read. Bash can be nicer to read, I was trying to be too clever probably :/

Leave a Reply