-1

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.

1

2 Answers 2

1

This would work:

$string = "'Like' this you 'get' your entire array in the 'variable' if the 'form'.";
$parts = explode("'",$string);
$isLeft = FALSE;
foreach ($parts as $key => $part) {
  if ($part != '') $parts[$key] = $isLeft ? '$#44'.$part : '$#55'.$part;
  $isLeft = !$isLeft;
}
echo implode('',$parts);

It's not pretty, but it does the job. The result is:

$#44Like$#55 this you $#44get$#55 your entire array in the $#44variable$#55 if the $#44form$#55.

Something with regular expressions might be a lot shorter, and perhaps better, but I have always difficulty understanding them. See: https://regex101.com

Sign up to request clarification or add additional context in comments.

3 Comments

tnx a lot bro..but help me for an another query..suppose if i have a string like $str = "'Like' this you 'get' your entire array in the 'variable' if the 'form."; i wanna the output $#44Like$#55 this you $#44get$#55 your entire array in the $#44variable$#55 if the 'form.
hello bro, thanks a lot for your help..now if i want to replace just for a string which have wrapped from both side like 'test' if it has one side single quote it will not replace ...even i wanna change " " at a time..Could you please help me?? output will like $#44Like$#55 this you $#44get$#55 your entire $#44array$#55 @in@ the $#44variable$#55 if the 'form. from the string of 'Like' this you 'get' your entire 'array' "in" the 'variable' if the 'form. @ will be replace in staed of " ". Note single " will not be changed...Please help me..please help me...
As Lawrence Cherone says: You shouldn't stray to far from the original question in your comments. I would probably have made other choices in my answer, had I known your new demands in advance. There are many ways you can do what you want, so be creative and think of one.
1

Deciphering your question to..

Suppose I have a string 'Like' this you 'get' your entire ..., and I want to replace every odd instance of ' with $#44 and every even instance with $#55.

<?php
$str = "'Like' this you 'get' your entire array in the 'variable' if the 'form'.";
$find = "'";

$result = null;
$odd = true;
for ($i=0; $i < strlen($str); $i++) {
    if ($str[$i] == $find) {
        $result .= $odd ? '$#44' : '$#55';
        $odd = !$odd;
    } else {
        $result .= $str[$i];
    }
}

echo $result;
?>

https://3v4l.org/EnJ9M

Result:

$#44Like$#55 this you $#44get$#55 your entire array in the $#44variable$#55 if the $#44form$#55.

Edit: OP wants to replace multiple sets.

<?php
$str = "'Like' this you 'get' your entire array \"in\" the 'variable' if the 'form'.";

// for each item, have a matching $replace pair
$find = [
    '\'', 
    '"'
];

$replace = [
    ['$#44', '$#55'], // replaces ' 
    ['@', '@']        // replaces "
];

$result = null;
$odd = true;
for ($i=0; $i < strlen($str); $i++) {
    if (in_array($str[$i], $find)) {
        $key = array_search($str[$i], $find);
        $result .= $odd ? $replace[$key][0] : $replace[$key][1];
        $odd = !$odd;
    } else {
        $result .= $str[$i];
    }
}

echo $result;
?>

https://3v4l.org/EuSaV

Result:

$#44Like$#55 this you $#44get$#55 your entire array @in@ the $#44variable$#55 if the $#44form$#55.

5 Comments

tnx a lot..its working now..but if want to replace " to & and " to # accordingly even odd..that would be the same code for its?
hello bro, thanks a lot for your help..now if i want to replace just for a string which have wrapped from both side like 'test' if it has one side single quote it will not replace ...even i wanna change " " at a time..Could you please help me?? output will like $#44Like$#55 this you $#44get$#55 your entire $#44array$#55 @in@ the $#44variable$#55 if the 'form. from the string of 'Like' this you 'get' your entire 'array' "in" the 'variable' if the 'form. @ will be replace in staed of " ". Note single " will not be changed...Please help me..please help me...
@XeonLunux See edit, dont forget to accept. Accepting Answers: How does it work?
i wanna replace left ' to #$44 and right ' to $#55. Like 'input'=$#44input$#55. but if 'input have in a single quatation ,it will not be changes.Same as double quotation " ". for both double quatation " " it will be replaced .for left " to @ and right " to &. example "input". it will be @input&. not be changed for "input or input". Hope you got my point what i want. example. 'this' is the 'best opportunity "to" get help" from the experts....output will be $#44this$#55 is the 'best opportunity @to& get help" from the experts...
Sorry but you keep moving the goal posts.. I answered the original question. Open another as it seems to have changed from replacing odd/even to something else. It was @in@ now its @in&!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.