How does php implement html tag closure detection and processing? This article mainly introduces the method of detecting and repairing html tag closures in PHP, which can detect and complete the closing tags in html tags. I hope to be helpful.
The example in this article describes the method of detecting and repairing html tag closure in PHP. Share it with everyone for your reference. The details are as follows:
html tag closure detection and repair, it is a bit big, and it is not considered completely. Regular expressions are not used. It is suitable for html files that only have start tags and no end tags. Yes, there is an end tag. There is no start tag. The closing position of the label needs to be adjusted according to the needs
<?php $str = ' <p data="<li></li>"> <img src="/static/imghwm/default1.png" data-src="http://www.baidu.com/123123.png" class="lazy" / alt="PHP implements html tag closure detection and processing" > <p2> <a>content</a> </p2> <ul> <li> </li> </ul> <p> content full </p> this is content</test1> this is content</test2> <test4 data="liujinjing"> This is cont <li></li> <test3 data="liujinjing"> This is content <p3> </p3> </p4> </p> </p> <p6 style="width:90px; "> this is content'; $str_len = strlen($str); //记录起始标签 $pre_data = array(); //记录起始标签位置 $pre_pos = array(); $last_data = array(); $error_data = array(); $error_pos = array(); $i = 0; //标记为 < 开始 $start_flag = false; while( $i < $str_len ) { if($str[$i]=="<" && $str[$i+1]!='/' && $str[$i+1]!='!') { $i++; $_tmp_str = ''; //标记为 < 开始 $start_flag = true; //标记空白 $space_flag = false; while($str[$i]!=">" && $str[$i]!="'" && $str[$i]!='"' && $str[$i] !='/' && $i<$str_len){ if($str[$i]==' ') { $space_flag = true; } if(!$space_flag) { $_tmp_str .= $str[$i]; } $i++; } $pre_data[] = $_tmp_str; $pre_pos[] = $i; } else if ($str[$i]=="<" && $str[$i+1]=='/') { $i += 2; $_tmp_str = ''; while($str[$i]!=">" && $i<$str_len){ $_tmp_str .= $str[$i]; $i++; } $last_data[] = $_tmp_str; //查看开始标签的上一个值 if(count($pre_data)>0) { $last_pre_node = getLastNode($pre_data, 1); if($last_pre_node == $_tmp_str) { //配对上, 删除对应位置的值 array_pop($pre_data); array_pop($pre_pos); array_pop($last_data); } else { //没有配对上, 有两种情况 //情况一: 只有闭合标签, 没有开始标签 //情况二:只有开始标签, 没有闭合标签 array_pop($last_data); $error_data[] = $_tmp_str; $error_pos[] = $i; } } else { array_pop($last_data); $error_data[] = $_tmp_str; $error_pos[] = $i; } }else if ($str[$i]=="<" && $str[$i+1]=="!") { $i++; while($i<$str_len) { if($str[$i]=="-" && $str[$i+1]=="-" && $str[$i+2]==">") { $i++; break; } else { $i++; } } $i++; }else if($str[$i]=='/' && $str[$i+1]=='>') { //跳过自动单个闭合标签 if($start_flag) { array_pop($pre_data); array_pop($pre_pos); $i+=2; } }else if($str[$i]=="/" && $str[$i+1]=="*"){ $i++; while($i<$str_len) { if($str[$i]=="*" && $str[$i+1]=="/") { $i++; break; } else { $i++; } $i++; } }else if($str[$i]=="'"){ $i++; while($str[$i]!="'" && $i<$str_len) { $i++; } $i++; } else if($str[$i]=='"'){ $i++; while($str[$i]!='"' && $i<$str_len ) { $i++; } $i++; } else { $i++; } } //确定起始标签的位置 function confirm_pre_pos($str, $pre_pos){ $str_len = strlen($str); $j=$pre_pos; while($j < $str_len) { if($str[$j] == '"') { $j++; while ($j<$str_len) { if($str[$j]=='"') { $j++; break; } $j++; } } else if($str[$j] == "'") { $j++; while ($j<$str_len) { if($str[$j]=="'") { $j++; break; } $j++; } } else if($str[$j]==">") { $j++; while ($j<$str_len) { if($str[$j]=="<") { //退回到原有内容位置 $j--; break; } $j++; } break; } else { $j++; } } return $j; } //确定起始标签的位置 function confirm_err_pos($str, $err_pos){ $j=$err_pos; $j--; while($j > 0) { if($str[$j] == '"') { $j--; while ($j<$str_len) { if($str[$j]=='"') { $j--; break; } $j--; } } else if($str[$j] == "'") { $j--; while ($j<$str_len) { if($str[$j]=="'") { $j--; break; } $j--; } } else if($str[$j]==">") { $j++; break; } else { $j--; } } return $j; } //获取数组的倒数第num个值 function getLastNode(array $arr, $num){ $len = count($arr); if($len > $num) { return $arr[$len-$num]; } else { return $arr[0]; } } //整理数据, 主要是向后看, 进一步进行检查 function sort_data(&$pre_data, &$pre_pos, &$error_data, &$error_pos){ $rem_key_array = array(); $rem_i_array = array(); //获取需要删除的值 foreach($error_data as $key=>$value){ $count = count($pre_data); for($i=($count-1) ; $i>=0; $i--) { if($pre_data[$i] == $value && !in_array($i, $rem_i_array)) { $rem_key_array[] = $key; $rem_i_array[] = $i; break; } } } //删除起始标签相应的值 foreach($rem_key_array as $_item) { unset($error_pos[$_item]); unset($error_data[$_item]); } //删除结束标签相应的值 foreach($rem_i_array as $_item) { unset($pre_data[$_item]); unset($pre_pos[$_item]); } } //整理数据, 闭合标签 function modify_data($str, $pre_data, $pre_pos, $error_data, $error_pos){ $move_log = array(); //只有闭合标签的数据 foreach ($error_data as $key => $value) { // code... $_tmp_move_count = 0; foreach ($move_log as $pos_key => $move_value) { // code... if($error_pos[$key]>=$pos_key) { $_tmp_move_count += $move_value; } } $data = insert_data($str, $value, $error_pos[$key]+$_tmp_move_count, false); $str = $data['str']; $move_log[$data['pos']] = $data['move_count']; } //只有起始标签的数据 foreach ($pre_data as $key => $value) { // code... $_tmp_move_count = 0; foreach ($move_log as $pos_key => $move_value) { // code... if($pre_pos[$key]>=$pos_key) { $_tmp_move_count += $move_value; } } $data = insert_data($str, $value, $pre_pos[$key]+$_tmp_move_count, true); $str = $data['str']; $move_log[$data['pos']] = $data['move_count']; } return $str; } //插入数据, $type 表示插入数据的方式 function insert_data($str, $insert_data, $pos, $type) { $len = strlen($str); //起始标签类型 if($type==true) { $move_count = strlen($insert_data)+3; $pos = confirm_pre_pos($str, $pos); $pre_str = substr($str, 0, $pos); $end_str = substr($str, $pos); $mid_str = "</" . $insert_data . ">"; //闭合标签类型 } else { $pos = confirm_err_pos($str, $pos); $move_count = strlen($insert_data) + 2; $pre_str = substr($str, 0, $pos); $end_str = substr($str, $pos); $mid_str = "<" . $insert_data . ">"; } $str = $pre_str.$mid_str.$end_str; return array('str'=>$str, 'pos'=>$pos, 'move_count'=>$move_count); } sort_data($pre_data, $pre_pos, $error_data, $error_pos); $new_str = modify_data($str, $pre_data, $pre_pos, $error_data, $error_pos); echo $new_str; // print_r($pre_data); // print_r($pre_pos); // print_r($error_data); // print_r($error_pos); // echo strlen($str); // foreach($pre_pos as $value){ // $value = confirm_pre_pos($str, $value); // for($i=$value-5; $i<=$value; $i++) { // echo $str[$i]; // } // echo "\n"; // } // foreach($error_pos as $value){ // for($i=$value-5; $i<=$value; $i++) { // echo $str[$i]; // } // echo "\n"; // } ?>
Related recommendations:
php Check whether the png image is Complete example code
php Function to detect file type
php Detect character encoding Code_PHP tutorial
The above is the detailed content of PHP implements html tag closure detection and processing. For more information, please follow other related articles on the PHP Chinese website!

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)