Reputation: 10708
I am trying to do some PHP programming concepts and I am not aware of some in-build functions. So my doubt is:
In PHP, how to remove slashes from strings? Is there any function available in PHP for this?
e.g.
$string = "people are using Iphone/'s instead of Android phone/'s";
Upvotes: 15
Views: 60658
Reputation: 1
I tried this method to remove single forward slashes.
I used str_replace
to strip the slashes out. It still did not work for me, I had to go and change all the double quotes in the database to single quotes, update the table, then change it back to double quotes for it to work. Weird.
str_replace('\\', '', $content)
Upvotes: 0
Reputation: 3348
Use varian preg
$string="people are using Iphone/'s instead of Android phone/'s";
echo $string = preg_replace('/\//', '', $string);
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/uIBINP" ></iframe>
Upvotes: 0
Reputation: 5043
backslashes need escaping
$newstr = "<h1>Hello \ fred</h1>";
echo str_replace('\\','',$newstr);
Upvotes: 5
Reputation: 5264
you can use function like
$string = preg_replace ("~/~", "", $string);
Upvotes: 2
Reputation: 1975
Heres what I use
function removeSlashes($string = '')
{
return stripslashes(str_replace('/', '', $string));
}
Test
echo $this->removeSlashes('asdasd/asd/asd//as/d/asdzfdzdzd\\hd\h\d\h\dw');
Output
asdasdasdasdasdasdzfdzdzdhdhdhdw
Upvotes: 2
Reputation: 88647
You can do a number of things here, but the two approaches I would choose from are:
Use str_replace()
:
$string = "people are using Iphone/'s instead of Android phone/'s";
$result = str_replace('/','',$string);
echo $result;
// Output: people are using Iphone's instead of Android phone's
If the slashes are backward slashes (as they probably are), you can use stripslashes()
:
$string = "people are using Iphone\\'s instead of Android phone\\'s";
$result = stripslashes($string);
echo $result;
// Output: people are using Iphone's instead of Android phone's
Upvotes: 42
Reputation: 234
You can use stripslashes() function.
<?php
$str = "Is your name O\'reilly?";
// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>
Upvotes: -7