Home  >  Article  >  Backend Development  >  How to remove tag attributes in php

How to remove tag attributes in php

藏色散人
藏色散人Original
2021-12-13 09:28:451639browse

php method to remove tag attributes: 1. Create a PHP sample file; 2. Remove the tag attributes through "preg_replace($del,"",$file);" and other methods.

How to remove tag attributes in php

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How to remove tag attributes in php?

Use PHP to remove some attributes in all html tags

<?php
set_time_limit(0);
function view_dir($dir)
{
$dp=opendir($dir); //打开目录句柄
//echo "<br>".$dir."<br><br>"; 
$path2=&#39;&#39;;
while ($file = readdir($dp)) //遍历目录
{
   if ($file !=&#39;.&#39;&&$file !=&#39;..&#39;) //如果文件不是当前目录及父目录
   {    
    $path=$dir."/".$file; //获取路径
    if(is_dir($path)) //如果当前文件为目录
    {
     view_dir($path);   //递归调用
    }
    else   //如果不是目录
    {
 
//echo &#39;<tr><td><table width="100%" border="0" cellspacing="0" cellpadding="0">&#39;;
 
   $path2 .= $path."\r\n";
   
   $c=file_get_contents($path);
//   $c = str_replace(&#39;http://4.28.99.196/chigb&#39;, "", $c);
//   $c1=array(&#39; tppabs="/style/book.css" rel="stylesheet"&#39;,&#39; tppabs="/chigb/up.gif"&#39;,&#39; tppabs="/chigb/1pix.gif"&#39;,&#39; tppabs="/chigb/left.gif"&#39;,&#39; tppabs="/chigb/right.gif"&#39;);
//   $c2 = str_replace($c1, "", $c);
 
$del=array("/tppabs=.+?[&#39;|\"]/i");
$c2 = preg_replace($del,"",$c);
   $c2 = str_replace(&#39;" >&#39;, &#39;">&#39;, $c2);
 
   file_put_contents($path,$c2);
//echo &#39;<td width="52%" height="25"><a href="&#39;.$path.&#39;">&#39;.$path.&#39;</a></td>&#39;;
//echo &#39;<td width="17%" align="center"><a href=&#39;.$_SERVER[&#39;SCRIPT_NAME&#39;].&#39;?cp=&#39;.$path.&#39;> 复制至XX</a></td>&#39;;
//echo &#39;<td width="31%" align="center"><b><a href=&#39;.$_SERVER[&#39;SCRIPT_NAME&#39;].&#39;?cmd=del&name=&#39;.$path.&#39;>删除</a></b></td>&#39;;
//echo &#39;</tr>&#39;;
//echo &#39;</table></td></tr>&#39;;
    }
   }
}
return $path2."\r\n";
closedir($dp);
} 
$dir=&#39;E:book\chigb\x&#39;;
echo view_dir($dir);
 
 
 
 
 
$file=&#39;<div id="m"><p id="lg"><img src="../img/baidu_sylogo1.gif" width="27px" height="12px" usemap="#mp"><map name="mp"><a shape="rect" coords="40,25,230,95" href="../yuanso/index.html" target="_blank" title="点此进入空间" ></map></p><p id="nv"><a href="../yuanso/index1.html">文字1</a> <b>文字2</b> <a href="../yuanso/index3.html">文字3</a></p></div>
&#39;;
$del=array("/name=.+?[&#39;|\"]/i","/src=.+?[&#39;|\"]/i","/id=.+?[&#39;|\"]/i","/width=.+?[&#39;|\"]/i","/height=.+?[&#39;|\"]/i","/usemap=.+?[&#39;|\"]/i","/shape=.+?[&#39;|\"]/i","/coords=.+?[&#39;|\"]/i","/target=.+?[&#39;|\"]/i","/title=.+?[&#39;|\"]/i");
$file = preg_replace($del,"",$file);//去除style样式
$file = str_replace(" ","",$file);//去除所有空格
$file = str_replace("<ahref=","<a href=",$file);//还原空格
echo $file;
 
 
?>

Recommended study: "PHP Video tutorial

The above is the detailed content of How to remove tag attributes in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn