Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize html tag completion and filtering of Web content by PHP

Shulou Source: shulou.com Published: 2022-05-31 13:03:25 10月01日 Update

This article mainly explains the "PHP how to achieve web content html tag completion and filtering", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "PHP how to achieve web content html tag completion and filtering" it!

If the html tags of your page content are not displayed completely, some form tags are incomplete and the page is confused, or some local html pages other than your content are included, we can write a function method to complete the html tags and filter out useless html tags.

Php makes HTML tags automatically complete, close, filter function method 1:

Code:

Function closetags ($html) {preg_match_all ('# iU', $html, $result); $closedtags = $result [1]; $len_opened = count ($openedtags); if (count ($closedtags) = = $len_opened) {return $html;} $openedtags = array_reverse ($openedtags); for ($iTun0; $I < $len_opened; $iTunes +) {if (! in_array ($openedtags [$I], $closedtags)) {$html. ='' } else {unset ($closedtags [array _ search ($openedtags [$I], $closedtags)]);} return $html;}

Closetags () parsing:

Array_reverse (): this function flips the order of the elements in the original array, creates a new array, and returns. If the second parameter is specified as true, the key name of the element remains unchanged, otherwise the key name will be lost.

Array_search (): array_search (value,array,strict), this function looks for a key value in the array like in_array (). If the value is found, the key name of the matching element is returned. If it is not found, false is returned. If the third parameter strict is specified as true, the key name of the corresponding element is returned only if the data type and value are the same.

Php makes HTML tags automatically complete, close, and filter function method 2:

Function checkhtml ($html) {$html = stripslashes ($html); preg_match_all ("/\ $val) {$string [$key] = shtmlspecialchars ($val);}} else {$string = preg_replace ('/ & (# (\ d {3L5} | x [a-fA-F0-9] {4}) | [a-zA-Z] [a-z0-9] {2} 5})) ) /','&\\ 1, str_replace (array ('&','','), array ('&','','), $string));} return $string;}

Checkhtml ($html) parsing:

Stripslashes (): the function removes the backslash added by the addslashes () function. This function is used to clean up data retrieved from a database or HTML form.

Thank you for your reading, the above is the content of "PHP how to achieve web content html tag completion and filtering". After the study of this article, I believe you have a deeper understanding of how PHP realizes web content html tag completion and filtering, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Tags content functions web pages elements data arrays methods learning code parameters pages closure consistency confusion three only famous players that is partial Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker Redmi vpn OPPO Reno