0

文字列のシングル クォーテーション (') を置き換えたい。

どうやらこれはうまくいきません...:

$patterns = array();
$replacements = array();
$patterns[0] = "'";
$patterns[1] = '\'';
$replacements[0] = 'Something';
$replacements[2] = 'Same thing just in a other way';
4

2 に答える 2

2

'( ) を ( ) に置き換えると"、 で問題なく動作しstr_ireplaceます。

$test = str_ireplace("'", "\"", "I said 'Would you answer me?'");
echo $test; // I said "Would you answer me?" 

"( ) を ( ')に置き換えても問題なく動作します

$test = str_ireplace("\"", "'", "I said \"Would you answer me?\"");
echo $test; // I said 'Would you answer me?' 
于 2010-02-15T15:14:42.250 に答える
0

サンプルコードが匿名化されすぎて($ replacesのインデックス0と2?)、切り捨てられすぎているようです(str_ireplace呼び出しはどこにありますか)...str_ireplaceを完全に理解していないと推測します。

最初のポイントは、str_ireplaceが適切に機能しないことです。戻り値は、変更された文字列/文字列の配列です。

2番目のポイントは、検索と置換の配列がある場合、PHPは、各配列から1つのアイテムを取得し、それをサブジェクト/サブジェクトの配列に適用してから、各配列から次のアイテムに移動して適用することです。同じ主題にそれ。これは、両方のサブジェクトが「'」を「何か」に置き換え、「同じものを別の方法で」が結果に表示されない以下の例で確認できます。

$patterns = array();
$replacements = array();
$patterns[0] = "'";
$patterns[1] = '\'';
$replacements[0] = 'Something';
$replacements[1] = 'Same thing just in a other way';

$subjects[0] = "I've included a single quote.";
$subjects[1] = "This'll also have a quote.";

$newSubjects = str_ireplace($patterns, $replacements, $subjects);

print_r($newSubjects);

実行すると、これは

配列([0]=>ISomethingveには一重引用符が含まれています。[1]=>ThisSomethingllにも引用符があります。)

于 2010-02-15T16:26:39.533 に答える