advanced-strreplace
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
/**
* @author Robert Deiman
* @copyright 2008
* @param $aToreplace Array This array contains the items to replace
* @param $aReplacewith Array This array contains the items that are the replacements
* @param $sReplacestring String This string needs to be the text that the replacements need to be used for
* @param $bReplacewords Boolean Set to true if you're having problems with replacements (strange results)
*/
function array_replace($aToreplace, $aReplacewith, $sReplacestring, $bReplacewords = true){
//first we need to order both the arrays for replacement in the right order, to prevent mistakes
//that means the arrays must keep the same keys for the item to replace, and to replace it with
array_multisort($aToreplace,$aReplacewith);
rsort($aReplacewith);
rsort($aToreplace);
//if it's not needed to strictly use one item, because no strange results appear
if(!$bReplacewords){
//just use str_replace
$sReturnvalue = str_replace($aToreplace,$aReplacewith,$sReplacestring);
}
//you get weird results, so try this other option
else{
//walk thru each item that needs replacement
foreach($aToreplace as $sReplaceKey => $sReplaceValue){
//check if the item that needs replacement is 100% the same
if($sReplaceValue == $sReplacestring){
$sReturnvalue = str_replace($sReplaceValue,$aReplacewith[$sReplaceKey],$sReplacestring);
break;
}
}
}
return $sReturnvalue;
}
/* UNDERNEATH THIS IS ONLY FOR EXAMPLE, NOT PART OF THE FUNCTION */
$sStartitem = 'micro/combi';
$aReplaceitems= array('micro/combi','micro');
$aReplacewith=array('microwave/combination','microwave');
//use of the standard str_replace function will return microwavewave/combination
echo str_replace($aReplaceitems,$aReplacewith,$sStartitem).'<br />';
//use of the array_replace funtion in it's standard settings will just return microwave/combination
echo array_replace($aReplaceitems,$aReplacewith,$sStartitem).'<br />';
//use of the array_replace function with the $bReplacewords set to false will also return microwavewave combination
echo array_replace($aReplaceitems,$aReplacewith,$sStartitem,false).'<br />';
?>
ini_set('display_errors', 1);
error_reporting(E_ALL);
/**
* @author Robert Deiman
* @copyright 2008
* @param $aToreplace Array This array contains the items to replace
* @param $aReplacewith Array This array contains the items that are the replacements
* @param $sReplacestring String This string needs to be the text that the replacements need to be used for
* @param $bReplacewords Boolean Set to true if you're having problems with replacements (strange results)
*/
function array_replace($aToreplace, $aReplacewith, $sReplacestring, $bReplacewords = true){
//first we need to order both the arrays for replacement in the right order, to prevent mistakes
//that means the arrays must keep the same keys for the item to replace, and to replace it with
array_multisort($aToreplace,$aReplacewith);
rsort($aReplacewith);
rsort($aToreplace);
//if it's not needed to strictly use one item, because no strange results appear
if(!$bReplacewords){
//just use str_replace
$sReturnvalue = str_replace($aToreplace,$aReplacewith,$sReplacestring);
}
//you get weird results, so try this other option
else{
//walk thru each item that needs replacement
foreach($aToreplace as $sReplaceKey => $sReplaceValue){
//check if the item that needs replacement is 100% the same
if($sReplaceValue == $sReplacestring){
$sReturnvalue = str_replace($sReplaceValue,$aReplacewith[$sReplaceKey],$sReplacestring);
break;
}
}
}
return $sReturnvalue;
}
/* UNDERNEATH THIS IS ONLY FOR EXAMPLE, NOT PART OF THE FUNCTION */
$sStartitem = 'micro/combi';
$aReplaceitems= array('micro/combi','micro');
$aReplacewith=array('microwave/combination','microwave');
//use of the standard str_replace function will return microwavewave/combination
echo str_replace($aReplaceitems,$aReplacewith,$sStartitem).'<br />';
//use of the array_replace funtion in it's standard settings will just return microwave/combination
echo array_replace($aReplaceitems,$aReplacewith,$sStartitem).'<br />';
//use of the array_replace function with the $bReplacewords set to false will also return microwavewave combination
echo array_replace($aReplaceitems,$aReplacewith,$sStartitem,false).'<br />';
?>