I wanto replace character in a string. I searched positions by a selected character and I find out the position of ' and finally got the position like 0,5,16,20,47,56,65,70. Now I want to replace all even position (0,16,20,56,70) with $#44 and 5,47,65 with $#55 of odd position.
$find_char="'";
$left_replace_char = '$#44';
$right_replace_char = '$#55';
$string="'Like' this you 'get' your entire array in the 'variable' if the 'form'.";
$positions = array();
$pos = -1;
while (($pos = strpos($string, $find_char, $pos+1)) !== false) {
$positions[] = $pos;
}
$result = implode(',',$positions);
print_r($result); echo "<br/>";
foreach ($positions as $index=>$value) {
if ($value % 2 == 0){
$valEven[]=$value;
} else {
$valOdd []= $value;
}
}
foreach ($valEven as $pos) {
$strings = substr_replace($string, $left_replace_char, $pos, 1);
}
foreach ($valOdd as $pos) {
$strings = substr_replace($string, $right_replace_char, $pos, 1);
}
echo $strings;
It does not work. Please fixed my problem.