简体   繁体   中英

How can I remove slashes from strings?

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??

eg

$string="people are using Iphone/'s instead of Android phone/'s";

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

backslashes need escaping

$newstr = "<h1>Hello \ fred</h1>";

echo str_replace('\\','',$newstr);

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

If it is a quoted string. Use stripslashes

你可以使用类似的功能

 $string = preg_replace ("~/~", "", $string);

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>

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)

You can use stripslashes() function.

<?php
$str = "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>

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