0

How do I change these codes using PHP code ...

I tried this but failed.

$str = str_replace(
array('A','/\'),
array('M','/\/\'),
array('N','/\/'),
array('V','\/'),
array('W','\/\/'),
$html);

What is the best practice for doing this?

2
  • What you are trying to change ? Commented May 11, 2019 at 7:45
  • @RakeshJakhar Of course the letters A, M, N, V, W Commented May 11, 2019 at 7:48

2 Answers 2

1

You need to escape the backslashes. Also, you are not forming the parameters to str_replace correctly, they should be an array of strings and an array of replacements. This will do what you want:

$html = 'MAVEN WAR';
$str = str_replace(array('A', 'M', 'N', 'V', 'W'), 
                   array('/\\','/\\/\\','/\\/','\\/','\\/\\/'), 
                   $html);
echo $str;

Output:

/\/\/\\/E/\/ \/\//\R

Demo on 3v4l.org

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

Comments

0

The correct way of using the str_replace is

$string = "ABCD This";
$pattern = ['A','B','C','D'];
$replacments = ['1A','2B','3C','4D'];
echo str_replace($pattern, $replacments, $string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.