preg_replace blues
Hi all, preg-Replace has stumped me again. I'm just too thick to get it.
This is my problem:
I have two files. File 'newvalues.php' with an array like so:
....
$lang['this_key'] => 'blah blah blah';
$lang['another_key'] => 'rhubarb rhubarb';
$lang['something_else_key'] => 'twitter twitter';
....
And another file: 'template.php' like so:
$lang = array(
....
'this_key' => 'some other value',
'something_else_key' => 'an old value',
'another_key' => 'spaghettiness',
....
};
What I need is some preg_replace stuff to replace the values in the template.php file with the values in the newvalues.php file.
Before you ask, no I have no control over the format, these are what I have to work with. I also HAVE to have the working file in the template.php format, even though the newvalues.php file would do the same job!
I've been working along the lines of
$newcontent = preg_replace($regex,$newvalue,$tmpfilecontents);
Unfortunately, it's the regex I've been struggling with. I trying to do this'a_key'(whitespace)=>(whitespace)'(content_to_replace)' for it, but I'm going around in circles.
Anybody shed some light on this for me?
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Perhaps it's easier to replace => with = in the first one and include it or them (in the correct order)?
twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
Sorry, my typo, it's supposed to be '=' in the first one. I have no control over the content of the files, so changing the order ins't an option unfortunately.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
COuld you elaborate, I don't follow.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Thanks for the reply. I don't think that'll work for me. The template.php file array values have to be overwritten with the newvalues.php values.
I'll just clarify. A translation website has been used to translate a series of files. The original files were in the template format. The website spat out a file in newvalues format. I'm just trying to mop up the problem. Although both files contain the same data, the template file is in the format accepted by the software developer. The files are being created for a well-know open source program.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
No, the output will be a file like template.php. Identical, except for the new values. This file is not meant to be displayed. I just need to be able to produce this file so that it can be submitted as a translation file.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Okay, some thoughts, I didn't test this:
First, you'll need to match the new values against something like "/\$lang\['([_a-z]+)'\]\s*=>\s*'([_\sa-z]+)'/i" (with preg_match_all, you'll need to use PREG_SET_ORDER).
Than, replace everything in the template:
if($matches) foreach($matches as $v) preg_replace("/'$v[1]'\s*=>\s*'[_\sa-z]+'/i", "'$v[1]' => '$v[2]'", $template);
twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
Thank you both, I will give your ideas a whirl and come back.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
foreach($lang as $key => $value){
$str = preg_replace("/'$key'\s*=>\s*'.*'/", "'$key' => '$value'", $str);
}
@twiss - that was amazing - thank you. The base pattern was there, but you won't believe how long it took for me to discover '.*' !! :)
@Ty - RegexBuddy looked promising until I saw the live demos. Nosebleed time! The 2nd link took me to QuickREx, which looks a far more sane proposition for me.
Thanks again guys. Problem solved.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080