簡體   English   中英

如何從字符串中刪除斜杠?

[英]How can I remove slashes from strings?

我正在嘗試做一些 PHP 編程概念,但我不知道一些內置函數。 所以我的疑問是:

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

例如

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

你可以在這里做很多事情,但我會選擇的兩種方法是:

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

反斜杠需要轉義

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

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

這是我使用的

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

測試

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

輸出

asdasdasdasdasdasdzfdzdzdhdhdhdw

如果是帶引號的字符串。 使用stripslashes

你可以使用類似的功能

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

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

我嘗試了這種方法來刪除單個正斜杠。

我使用str_replace斜線。 它仍然對我不起作用,我不得不將數據庫中的所有雙引號更改為單引號,更新表,然后將其更改回雙引號才能工作。 奇怪的。

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

您可以使用 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