php - How can I detect and print what characters str_replace() changed? -
i'm using code replace words each other.
in scenario, in same time...just there 1 same word in #old , $string example $string = "da"
, $string = "fjb"
true $string = "ba"
or $string = "dahjc"
false.
$string = "sdfsdfsdfa" $old = array("a", "b", "c"); $new = array("1", "2", "3"); $string = str_replace($old, $new, $string);
how can detect characters got replaced str_replace()
, print this:
result: 'a' replaced '1'
<?php $oldstring = "sdfsdfsdfa"; $main_array=array("a"=>"1","b"=>"2", "c"=>"3"); $old=array_keys($main_array); $new=array_values($main_array); $newstring = str_replace($old, $new, $oldstring); foreach ($main_array $key => $value) { if (strpos($oldstring,$key) !== false) { echo "result: {$key} replaced {$value} <br>"; } } echo '<b>'.$oldstring.'</b><br>'; echo $newstring; ?>
Comments
Post a Comment