-
-
Save jimmytuc/f67003bc23198acc8b39 to your computer and use it in GitHub Desktop.
PHP str_replace customization to adapt multi array element passing as argument1 & argument2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function p_str_replace($argFind, $agrReplace, $theString) { | |
$needle_replace = $theString; // handle the string need to be replace | |
array_walk($argFind, function($val, $key) use(&$needle_replace, $agrReplace) { | |
$needle_replace = call_user_func_array('str_replace', array($val, $agrReplace[$key], $needle_replace)); | |
}); | |
return $needle_replace; | |
} | |
// testing purpose | |
$the_str = array( | |
'coa' => ':col: :op1: :val: AND :col: :op2: :val:' | |
); | |
$ope = array('>=', '<='); | |
$colsdata = array('date_start', '2015-10-14'); | |
echo p_str_replace(array( | |
array(':col:', ':val:'), | |
array(':op1:', ':op2:') | |
), array( | |
$colsdata, | |
$ope | |
), $the_str['coa']); | |
echo '-------------------------------' . "\n"; | |
//some complexity | |
$complex = '(:col: :op1: :val: AND :col: :op2: :val:) AND (:col2: :op1: :val2: AND :col2: :op1: :val2:)'; | |
echo p_str_replace(array( | |
array(':col:', ':val:'), | |
array(':col2:', ':val2:'), | |
array(':op1:', ':op2:') | |
), array( | |
array('date_start', '2015-10-01'), | |
array('date_end', '2015-10-14'), | |
array('>', '<') | |
), $complex); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment