テキストへの正しい告知または閉じられていないタグを閉じる

ブログのメインページに記事を追加して記事を一覧表示した後、発表に関する質問が発生しました。 特定の数の文字をトリミングすると、テキストエディターによって追加されたHTMLタグもトリミングされます。 この「アナウンス」から、レイアウトは継ぎ目でatっています。 そして、もちろん、私はそれが好きではありません。 良い服装の後、この点で解決策が見つかりました。 正しいアナウンスを作成する方法は次のとおりです。





function anons($html = '') { $html = trim($html); $anons_tag = "<!-- my-anons -->"; preg_match('/^(.*)' . $anons_tag . '/SUsi',$html,$res); if(!empty($res[1])) $html = $res[1]; #put all opened tags into an array preg_match_all("##iU",$html,$result); $openedtags = array_unique($result[1]); #put all closed tags into an array preg_match_all("##iU",$html,$result); $closedtags = array_unique($result[1]); $len_opened = count($openedtags); # all tags are closed if(count($closedtags) == $len_opened){ return $html; } $openedtags = array_reverse($openedtags); # close tags for($i=0;$i < $len_opened;$i++) { if (!in_array($openedtags[$i],$closedtags)){ $html .= ''; } else { unset($closedtags[array_search($openedtags[$i],$closedtags)]); } } return $html; }
      
      







$ anons_tag変数は、アナウンスメントタグのテキストを格納します。 彼は何でもかまいません。 アナウンスメントの最後に挿入します。



All Articles