2017-08-01 9 views
-1

htmlspecialcharsを使用したいが、大きな(>)と小さい(<)記号を変換したい。私はシングル、ダブル、 '&'の記号を変換したくない。私は試して検索しましたが、答えを見つけることができませんでした。助けてください。htmlspecialcharsより大きい(>)と小さい(<)記号だけを変換する

+1

ていますか?なぜ説明できますか?未加工のアンパサンドは無効なHTMLです – Eric

答えて

0

は、「干し草の山の中の針」などの配列でstr_replace()関数を使用してみてください:

$search_for_lt = array("<","&lt;"); 
$string = str_replace($search_for_lt, "replacement here, can be empty", $string); 
$search_for_gt = array(">","&lt;"); 
$string = str_replace($search_for_gt, "replacement here, can be empty", $string); 

あなたが期待通りに動作されていることを確認することはできますか?

0

str_replaceを使用している文字だけを置き換えるのはなぜですか。 str_replacestrtr

$result = str_replace(['<', '>'], ['&lt;', '&gt;'], $string); 
2
$replace = array('<' => '&lt;', '>' => '&gt;'); 
$string=strtr($string, $replace); 

違いがここで説明されています。あなたはそれをやってもよろしいWhen to use strtr vs str_replace?

関連する問題