PHP replace tags with dynamic values including IF condition -
replacing dynamic values in php can achieved using code this:
$replace = array('{cover_amt}','{liability_amt}','{total_amt}'); $with = array('90', '90', '0'); $mystring = 'this cover amt : {cover_amt} . liablity amount : {liability_amt} . total amount : {total_amt}'; echo str_replace($replace, $with, $mystring);
output:
this cover amt : 90 . liablity amount : 90 . total amount : 0
this give correct output.
but when value 0 should not displaying text itself. case, total amount should not displayed @ 0.
check if condition not great solution mess code if there lots of ‘0’s .
if there around 100 array elements, impossible check each values. solution can used number of data inputs great.
any 1 great idea achieve this.
thanks.
why not trying this:
<?php $replace = array('{cover_amt}','{liability_amt}','{total_amt}', 'this total amount : 0'); $with = array('90', '90', '0',''); $mystring = 'this cover amt : {cover_amt} . liablity amount : {liability_amt} . total amount : {total_amt}'; echo str_replace($replace, $with, $mystring); ?>
it delete last part if total amount 0.
acording edited question:
<?php $replace = array('{cover_amt}','{liability_amt}','{total_amt}'); $with = array('0', '90', '0'); $mystring = 'this cover amt : {cover_amt} . liablity amount : {liability_amt} . total amount : {total_amt}'; $mystring = str_replace($replace, $with, $mystring); $mystring_array = explode("this is",$mystring); foreach($mystring_array $mystring_sliced) { $pattern = '/(\.?)(this )(.*?) 0(\s?)(\.?)/i'; $replacement = ''; if($mystring_sliced) echo preg_replace($pattern, $replacement, "this ".$mystring_sliced); } ?>
Comments
Post a Comment