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

时间:2011-11-25 08:37:22

标签: php

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

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

e.g。

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

8 个答案:

答案 0 :(得分:29)

你可以在这里做很多事情,但我选择的两种方法是:

使用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

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

答案 1 :(得分:3)

反斜杠需要转义

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

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

答案 2 :(得分:2)

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

答案 3 :(得分:1)

继承人我使用的

function removeSlashes($string = '')
{
    return stripslashes(str_replace('/', '', $string));
}

测试

echo $this->removeSlashes('asdasd/asd/asd//as/d/asdzfdzdzd\\hd\h\d\h\dw');

输出

asdasdasdasdasdasdzfdzdzdhdhdhdw

答案 4 :(得分:0)

您可以使用

之类的功能
 $string = preg_replace ("~/~", "", $string);

答案 5 :(得分:0)

使用varian preg

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

echo $string = preg_replace('/\//', '', $string);

&#13;
&#13;
body, html, iframe { 
  width: 100% ;
  height: 100% ;
  overflow: hidden ;
}
&#13;
<iframe src="https://ideone.com/uIBINP" ></iframe>
&#13;
&#13;
&#13;

答案 6 :(得分:0)

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

我用str_replace去除了斜线。它仍然对我不起作用,我不得不去将数据库中的所有双引号都更改为单引号,更新了表,然后将其更改回双引号才能正常工作。奇怪的。

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

答案 7 :(得分:-1)

您可以使用stripslashes()函数。

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

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