1

I would like to replace array string values which contains multiple special characters to normal one.

Tried Code (array values):

$data['ENV_TEST'] = "rambo";
$data['ENV_DEV'] = "Project Bribara<"${ENV_TEST}"@gmail.com>"

echo str_replace("${ENV_TEST}", $data['ENV_DEV'], $data['ENV_DEV']);

also tried

echo str_replace("\"${ENV_TEST}\"", $data['ENV_DEV'], $data['ENV_DEV']);

Expected:

"Project Bribara<[email protected]>"

Actual:

"Project Bribara<"${ENV_TEST}"@gmail.com>"

How can I get the expected output?

2 Answers 2

1

You should on PHP strings sometime. The important part about double quoted strings for your question is that you need to put a backslash before every $ and every " inside your string. Your code will then look like this:

$data['ENV_TEST'] = "rambo";
$data['ENV_DEV'] = "Project Bribara<\"\${ENV_TEST}\"@gmail.com>";

echo str_replace("\${ENV_TEST}", $data['ENV_TEST'], $data['ENV_DEV']);
//also tried
echo "\n\n";
echo str_replace("\"\${ENV_TEST}\"", $data['ENV_TEST'], $data['ENV_DEV']);

If you use single quoted strings you don't need to escape $ (see the manual), and instead of \", you would need to escape single quotes (but there aren't any in your example).

$data['ENV_TEST'] = "rambo";
$data['ENV_DEV'] = 'Project Bribara<"${ENV_TEST}"@gmail.com>';

echo str_replace("\${ENV_TEST}", $data['ENV_TEST'], $data['ENV_DEV']);
//also tried
echo "\n\n";
echo str_replace('"${ENV_TEST}"', $data['ENV_TEST'], $data['ENV_DEV']);

I also fixed a missing semicolon and replaced DEV with TEST in one place.

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

Comments

1

String concatenation in PHP is done through the . operator. Your code would be:

$data['ENV_DEV'] = "Project Bribara<".$data['ENV_TEST']."@gmail.com>"

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.