Sheriff Said Elahl Sheriff Said Elahl - 2 years ago 86
PHP Question

how to add double quotation character in PHP string using str_replace

I have String in PHP like:


and I want to change it to

["x", "y", "z"]

I used str_replace but I can't represent the double quotation mark " in it like this

$modified = str_replace("[", "["", $NodeIDs);

I also used \ before it like java but it appears in the output. how can I do this?

Answer Source

You can use double quotes " inside single quotes ':

$modified = str_replace("[", '["', $NodeIDs);

Or escape them:

$modified = str_replace("[", "[\"", $NodeIDs);

Or this might be a better approach to get the desired result:

$letters = explode(',', trim($NodeIDs, '[]'));
$NodeIDs = '["' . implode('","', $letters) . '"]';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download