<code> 削除するもの </code> 内のコンテンツに対してのみ htmlentities() を実行したい
文字列を受け取り、 <code> </code> の間のコンテンツを見つける関数を作成しました
function parse_code($string) {
// test the string and find contents with <code></code>
preg_match('@<code>(.*?)</code>@s', $string, $matches);
// return the match if it succeeded
if($matches) {
return $matches[1];
}else {
return false;
}
}
ただし、実際に htmlentities(); を実行する関数について助けが必要です。<code> </code> 内のコンテンツを処理し、implode() ですべてを元に戻します。たとえば、以下の文字列があるとします。
<div class="myclass" rel="stuff"> ここにあるもの </div> <code> ここでは htmlentites() のみを実行するため、 < > " ' & </code> などを削除します <div> ここにあるもの </div>
繰り返しますが、関数は文字列のすべての内容を同じに保つ必要がありますが、変更して <code> </code> の内容に対してのみ htmlentities() を実行する必要があります。