开发者

PHP replace ereg(i)(_replace) globally

开发者 https://www.devze.com 2023-04-09 01:25 出处:网络
I have开发者_StackOverflow社区 150+ PHP files I need to change (updating ereg to preg_match). I tired to update them manually but it takes for ever and I want to make sure all my replace will work the

I have开发者_StackOverflow社区 150+ PHP files I need to change (updating ereg to preg_match). I tired to update them manually but it takes for ever and I want to make sure all my replace will work the first time. What can I do to do this kind of operation?

Here's some example of my ereg(i)

if(eregi("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$", $ip)) { /* ... */}
if (eregi($regexp, $label, $match)) { /* ... */}
$string = eregi_replace("[[:space:]]+", ' ', $string);

Thanks


#!/bin/bash
perl -p -i -e "s/eregi_replace *\\( *\"([^\@]+?)(?<\!\\\\)\", */preg_replace(\"\@\\1\@i\", /g" $1
perl -p -i -e "s/eregi_replace *\\( *\'([^\@]+?)(?<\!\\\\)\', */preg_replace(\'\@\\1\@i\', /g" $1
perl -p -i -e "s/ereg_replace *\\( *\"([^\@]+?)(?<\!\\\\)\", */preg_replace(\"\@\\1\@\", /g" $1
perl -p -i -e "s/ereg_replace *\\( *\'([^\@]+?)(?<\!\\\\)\', */preg_replace(\'\@\\1\@\', /g" $1
perl -p -i -e "s/eregi *\\( *\"([^\@]+?)(?<\!\\\\)\", */preg_match(\"\@\\1\@i\", /g" $1
perl -p -i -e "s/eregi *\\( *\'([^\@]+?)(?<\!\\\\)\', */preg_match(\'\@\\1\@i\', /g" $1
perl -p -i -e "s/ereg *\\( *\"([^\@]+?)(?<\!\\\\)\", */preg_match(\"\@\\1\@\", /g" $1
perl -p -i -e "s/ereg *\\( *\'([^\@]+?)(?<\!\\\\)\', */preg_match(\'\@\\1\@\', /g" $1

Here's a little bash script that I use. Someone with more time than me can probably squash all of this into less (1?) regular expressions, but it should do the trick. Feel free to replace @ with your delimiter of choice. Just paste this into a file, chmod +x it, and then call it, passing the file as an argument.

If you need to use it en masse, something like this should suffice:

find /path/to/your/project -name '*.php' -exec foo {} \;

Where foo is whatever you named the bash script.

Note: This isn't perfect, so you'll still need to manually change any fringe cases that it misses, but it will still save you a lot of work. Also note that this does in-place file edits.


replacing the actual ereg call would be a fairly simple matter of doing a search/replace operation. But actually fixing up your pattern to include the delimiters that preg requires is somewhat non-trivial. You can easily do:

$newcode = str_replace('eregi("', 'preg_match("/', $oldcode));

but then you'll still have to find where the pattern ends to add a delimiter there, as well as the i modifier. And you'd have to do this for every variant of ereg calls as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消