PHP マニュアルから次の文を引用しました。
'this is a simple string',
'Arnold once said: "I\'ll be back"',
'You deleted C:\\*.*?',
'You deleted C:\*.*?',
'This will not expand: \n a newline',
'Variables do not $expand $either'
エスケープされた一重引用符 (2 番目の文のように) と二重のバックスラッシュ (3 番目の文のように) を使用して、表示されているとおりに PHP コードを使用してそれらをエコーしたいと思います。これは私がこれまでに持っているものです:
<?php
$strings = array(
'this is a simple string',
'Arnold once said: "I\'ll be back"',
'You deleted C:\\*.*?',
'You deleted C:\*.*?',
'This will not expand: \n a newline',
'Variables do not $expand $either');
$patterns = array('~\\\'~', '~\\\\~');
$replacements = array('\\\\\'', '\\\\\\\\');
foreach($strings as $string)
{
echo '\'' . preg_replace($patterns, $replacements, $string) . '\'' . '</br>';
}
?>
出力は次のとおりです。
'this is a simple string'
'Arnold once said: "I\\'ll be back"'
'You deleted C:\\*.*?'
'You deleted C:\\*.*?'
'This will not expand: \\n a newline'
'Variables do not $expand $either'
可能であれば、コードにリストされているとおりに文字列をエコーしたいと思います。2 つのバックスラッシュ文字 (\) に問題があります。私の 2 番目のパターン ('~\\~') は、単一のバックスラッシュと二重のバックスラッシュの両方を置き換えているようです。同じ結果で addcslashes() も使用してみました。
(私は最近他の場所でこの質問をしましたが、解決策はありません)
前もって感謝します。