php:想从字符串中替换'\\\ /'

I wanted to replace en/us with es/es:

<?php

$str = array('url'=>'www.domain.com\/data\/en\/us\/data.gif');

$json = json_encode($str);

$str = str_replace('en\/us', 'es\/es', $json);

echo $str;

You need to 'double escape' the backslash, like so:

<?php

$str = array('url'=>'www.domain.com/data/en/us/data.gif');

$json = json_encode($str);

$str = str_replace('en\\/us', 'es\\/es', $json);

echo $str;

See http://php.net/manual/en/language.types.string.php (section 'Single quoted').

Would be easier to escape the string BEFORE feeding it to json_encode, but I'm assuming this is a test case and the data you want to replace in is already JSON.

JSON is a useful format for moving data between systems. Converting data to JSON and then trying to manipulate it without parsing it first is almost always a terrible (overly complicated and error prone) idea.

Do the replacement before you convert it to JSON.

<?php

function replace_country($value) {
    echo $value;
    echo "
";
    return str_replace('en\/us', 'es\/es', $value);
}

$str = array('url'=>'www.domain.com\/data\/en\/us\/data.gif');
$str = array_map("replace_country", $str);
$json = json_encode($str);
echo $json;

Try this

$str = array('url'=>'www.domain.com\/data\/en\/us\/data.gif');
$str['url']=str_replace('en\/us', 'es\/es', $str['url']);
$json = json_encode($str);

It produce out put as

enter image description here

It will work for you.