📜 ⬆️ ⬇️

The correct announcement to the text or close the unclosed tags

After adding an article and displaying a list of articles on the main page of your samopisny blog, a question arose about the announcement. If you cut a certain number of characters, the HTML tags added by a text editor are also cut off. From this "announcement" imposition creeps at the seams. And, of course, does not suit me. After not weakness in this regard, a solution was found. Here it is a method for creating the correct announcement.


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; } 


The variable $ anons_tag stores the text of the announcement tag. He can be any. You insert it at the end of the announcement.

')

Source: https://habr.com/ru/post/145196/


All Articles