簡體   English   中英

從字符串中刪除尾部反斜杠

[英]remove trailing backslash from string

我想在不使用stripslashes()str_replace()情況下從字符串中刪除尾部反斜杠。 理想情況下,我將能夠使用rtrim() ,但它與反斜杠有關,使 PHP 嚇壞了。

$string = "This is my string\";

//iv'e tried with no success
$clean_string = rtrim($string, "\\");
$clean_string = rtrim($string, "\\\\");

理想情況下,字符串應該只是讀取“這是我的字符串”,末尾沒有反斜杠。 我不完全確定如何正確逃脫它,非常感謝任何幫助。

試試這個:

 if(substr($string, -1) == "\"){ 
   echo substr($string, 0, -1);
 }

if 條件檢查最后一個字符是否有斜線。

你可以試試preg_replace

$string = 'This is my string\\';
$clean_string = preg_replace('/(.+)(\\\\)$/', '${1}', $string);

如果模式匹配,尾部斜杠將被刪除。 否則,您將獲得相同的字符串。

這對我有用:

    if(substr($string, -1) == '\\')
    { 
        $string = substr($string, 0, -1);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM