简体   繁体   中英

str_replace not working in my php string

I want to remove a special character from a string in my php page, for that I use str_replace() function. But it does't work for my script. The string is getting from server. I am using the following php code to replace that string.

$path= "catalog\/demo\/samsung_tab_1.jpg";
$newPath = str_replace("\/","/",$path);

But the above str_replace() function is not working properly in my script.

I want to get the output like,

catalog/demo/samsung_tab_1.jpg

Please help.

Instead of \\/ you can remove forward slash by using double backslashes:

<?php
$path= "catalog\/demo\/samsung_tab_1.jpg";
$newPath = str_replace("\\","",$path); // replace with empty string ""
echo $newPath; // catalog/demo/samsung_tab_1.jpg
?>
<?php
$path= "catalog\/demo\/samsung_tab_1.jpg";
if (preg_match('/\//', $path)){
    echo $newPath = str_replace("\/","/",$path);
}else{
    echo $newPath = $path;
}
?>

I hope this will work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM