Member Avatar for diafol

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?

Perhaps it's easier to replace => with = in the first one and include it or them (in the correct order)?

Member Avatar for diafol

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.

Member Avatar for diafol

COuld you elaborate, I don't follow.

You just want to use the array values of newvalues.php to template.php.
I am not sure if i am correct or not.
Check below code.

<?
	include('newvalues.php');
	
	echo '<h2>New Values</h2>';
	echo '<pre>';
	print_r($lang);
	
	$langTemp = array(
	'this_key'  =>  'some other value',
	'something_else_key' => 'an old value',
	'another_key'  => 'spaghettiness');
	echo '<h2>Before replace</h2>';
	echo '<pre>';
	print_r($langTemp);
	
	foreach($langTemp as $key=>$val)
	{
		$langTemp[$key] = $lang[$key];
	}
	
	echo '<h2>After replace</h2>';
	echo '<pre>';
	print_r($langTemp);
?>
Member Avatar for diafol

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.

In both page array name ie $lang will be same?
You mean you can't modify template.php and newvalues.php's php code.
You want third php file which will take both file's array as an input.
What will be output? A string?
Sorry but one example of input and output will be more clear. :)

Member Avatar for diafol

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.

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);
commented: Great pattern - thank you :) +13

Sorry that this is not completely specific to your problem, but since you said "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."

Might I suggest something like Regex Buddy? Regex always hurts my head, and I find that the tool is something that reduces that from the level of a nasty migraine to a run-of-the-mill headache.

Only problem is that it's not free. So you may want to check out this thread for some alternatives.

commented: Good links - cheers +13
Member Avatar for diafol

Thank you both, I will give your ideas a whirl and come back.

Member Avatar for diafol
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.