I have a situation where i need to loop through various XML documents and replace certain values with xPath. So far so good, but the approach i'm using just feels wrong (but it works, that's at least something).
What i did is i created a multidimensional array for each replacement type (in order to keep it manageable) with the replacements in it. Then i loop through these arrays and push the values to seperate arrays wich i use to replace the values with. Like this:
$regions_original = Array();
$regions_replaced = Array();
$region_replacements = Array(
"Wrong spelled area 1" => "Area 1",
"Wrong spelled area 2" => "Area 2",
"Wrong spelled area 3" => "Area 3"
);
/*
* Split regions key/value pairs into two seperate arrays
*/
foreach($region_replacements as $original => $replacement){
array_push($regions_original, $original);
array_push($regions_replaced, $replacement);
}
$terms = Array('Area 1','Area 1','Wrong spelled area 1','Area 2','Wrong spelled area 2');
foreach($terms as $term){
echo '<li>'.str_replace($regions_original, $regions_replaced, $term).'</li>';
}
Looking at this makes me sick.. It just doesn't feel right. Also this approach also replaces occurrences within a sentence (when that's not what i want, see example below) so i thought of another approach by checking if the array key exists in the initial array and replace the value if it does. Like this:
foreach($terms as $term){
if(array_key_exists($term, $region_replacements)){
echo '<li>'.$region_replacements[$term].'</li>';
}else{
echo '<li>'.$term.'</li>';
}
}
This works, and even better than the first approach because with the first approach things like the following term are getting messed up:
$terms = array('Sainte Foy');
$replacements = array('Sainte Foy' => 'Domaine de Sainte Foy Tarantaise');
$output = echo '<li>'.str_replace($regions_original, $regions_replaced, $term).'</li>';
//Outputs: Domaine de Domaine de Sainte Foy Tarantaise Tarantaise
The thing with the last approach is that it is slower to execute. I've tested it with an XML file of about 500 elements and the difference is around 0.3sec in execution time (measured by comparing the microtime at the top and the bottom of the document). Because the source XML files can get a lot bigger i want the code to be efficient and managable as possible.
The second approach however 'feels' a lot better, although i can't stand the (apparantly) inefficiency. I hope that anyone can ether reassure me that one of these methods is fine or point me in a right direction to make it truly efficient. Would be great! :)