2

I want to remove a special character from a string; for that I use the str_replace() function. However, it doesn't work for my script.

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

But no replacements are made.

I want to get the output like:

catalog/demo/samsung_tab_1.jpg
4
  • 1
    its giving me this catalog/demo/samsung_tab_1.jpg means working.. Commented Oct 15, 2016 at 6:25
  • what is the result of echo $newPath; ? Commented Oct 15, 2016 at 6:28
  • @devpro catalog\/demo\/samsung_tab_1.jpg Commented Oct 15, 2016 at 6:32
  • try with // double backslashes as mentioned in answer and replace with "" Commented Oct 15, 2016 at 6:34

2 Answers 2

2

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
?>
Sign up to request clarification or add additional context in comments.

2 Comments

@Akhil: are u using $path or $newPath where you want to print?
@Akhil: and i hope you are using .php extension.
0
<?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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.