Preg_replace help

Status
Not open for further replies.

Tango

Moderator
Staff member
4,110
2009
1,747
20,605
Im scanning 1000's of files and want to replace certain values to 0



The values here 750,15 need to be changed to 0 and pushshort changed to pushbyte.

pushshort could be between 1-10,000
time could be between 1-10,000
The whole code needs to be matched so it doesnt edit the wrong sections in files.


PHP:
    pushstring          "costs"
    pushstring          "r1"
    pushshort           750
    pushstring          "r2"
    pushbyte            0
    pushstring          "r3"
    pushbyte            0
    pushstring          "r4"
    pushbyte            0
    pushstring          "time"
    pushbyte            15




And any number in this set to 0, and pushshort changed to pushbyte.

PHP:
    pushstring          "repairTime"
    pushbyte            30
    pushbyte            60
    pushbyte            120
    pushshort           240
    pushshort           480
    pushshort           960
    pushshort           1920
    pushshort           3840
    pushshort           7680
    pushshort           15360

Thanks
 
3 comments
Are these files layed out as one line for each variable/value? Are the files separate meaning that the first line with pushstring "costs" or pushstring "repairTime" would never be in same file?

If the answers are yes than I would read the folders and files as sequential data and either create new files or rewrite the lines updating the line based on your criteria and what is in the first line.
 
I think you are better of running PHP's exec command since PHP isn't really designed to manipulate files (eg find and replace):
PHP:
exec('perl -w -i -p -e "s/search_text/replace_text/g" foldercontainintallfiles');
 
I done it, it was easier than i thought :)


PHP:
$result1 = preg_replace('/([a-z]{10}\s*"[a-z1-4]{1,4}"\s*\n*[a-z]{1,15}\s*[0-9]{1,10}\n*\s*){5}/', 

   'pushstring          "r1"
    pushbyte            0
    pushstring          "r2"
    pushbyte            0
    pushstring          "r3"
    pushbyte            0
    pushstring          "r4"
    pushbyte            0
    pushstring          "time"
    pushbyte            0 ', $text);
 
Last edited:
Status
Not open for further replies.
Back
Top