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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8266476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:19:04  来源:igfitidea点击:

How can I remove slashes from strings?

php

提问by Kanika

I am trying to do some PHP programming concepts and I am not aware of some in-build functions. So my doubt is:

我正在尝试做一些 PHP 编程概念,但我不知道一些内置函数。所以我的疑问是:

In PHP, how to remove slashes from strings? Is there any function available in PHP for this??

在 PHP 中,如何从字符串中删除斜杠?PHP中是否有任何可用的功能?

e.g.

例如

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

采纳答案by manan

You can use stripslashes() function.

您可以使用 stripslashes() 函数。

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

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

回答by DaveRandom

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

回答by zzapper

backslashes need escaping

反斜杠需要转义

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

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

回答by abhinav

If it is a quoted string. Use stripslashes

如果是带引号的字符串。用stripslashes

回答by Adam

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

回答by vikky

you can use function like

你可以使用类似的功能

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

回答by antelove

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>

回答by Farid

I tried this method to remove single forward slashes.

我尝试了这种方法来删除单个正斜杠。

I used str_replaceto 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把斜线去掉。它仍然对我不起作用,我不得不将数据库中的所有双引号更改为单引号,更新表,然后将其更改回双引号才能工作。奇怪的。

str_replace('\', '', $content)