簡體   English   中英

從字符串中刪除2個字符之間的反斜杠

[英]Remove Backslash between 2 characters from string

我只需要在2個字符之間替換反斜杠,引號(“)除外

因此,如果我有這個STRING:

When I look at you, I\understand why I live //replace
When I look at you, I "\understand why I live // No replace
When I look at you, I"\understand why I live // No replace
Sword art online\Мастера меча онлайн opening //replace Sword art online Мастера меча онлайн opening

這是一個json字符串,但是如果我使用反斜杠,則會刪除所有反斜杠。 如果字符串no帶有引號,則只需刪除。

非常感謝。

您可以使用此:

$text = preg_replace('~"[^"]*"\K|\\\\~', '', $text);

或這一個:

$text = preg_replace('~"[^"]*"(*SKIP)(*F)|\\\\~', '', $text);

這兩種模式使用引號之間的字符。 第一種模式使用\\K從匹配結果中刪除所有在左側匹配的字符。 第二個強制模式失敗(使用(*F) ),並且不重試引號之間的字符(使用(*SKIP) )。

請注意,文字反斜杠必須在模式字符串中寫為\\\\\\\\ (反斜杠對於字符串和正則表達式引擎均轉義一次)。

嘗試這個:

$strings = array(
    'When I look at you, I\understand why I live',
    'When I look at you, I "\understand why I live',
    'When I look at you, I"\understand why I live',
    'Sword art online\Мастера меча онлайн opening'
);

foreach ($strings as $string) {
    $str = addslashes(stripslashes($string));
    var_dump($str);
}

暫無
暫無

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

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