简体   繁体   English

如何从字符串中删除斜杠?

[英]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.我正在尝试做一些 PHP 编程概念,但我不知道一些内置函数。 So my doubt is:所以我的疑问是:

In PHP, how to remove slashes from strings?在 PHP 中,如何从字符串中删除斜杠? Is there any function available in PHP for this?? PHP中是否有任何可用的功能?

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() :使用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() :如果斜杠是反斜杠(它们可能是),您可以使用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使用stripslashes

你可以使用类似的功能

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

Use varian preg使用 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.我使用str_replace斜线。 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.您可以使用 stripslashes() 函数。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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