• 技术文章 >后端开发 >PHP问题

    php怎么去除标签的属性

    藏色散人藏色散人2021-12-13 09:28:45原创536

    php去除标签属性的方法:1、创建一个PHP示例文件;2、通过“preg_replace($del,"",$file);”等方法去除标签的属性即可。

    php入门到就业线上直播课:进入学习

    本文操作环境:Windows7系统,PHP7.4版,Dell G3电脑。

    php怎么去除标签的属性?

    用PHP 去掉所有html标签里的部分属性

    <?php
    set_time_limit(0);
    function view_dir($dir)
    {
    $dp=opendir($dir); //打开目录句柄
    //echo "<br>".$dir."<br><br>"; 
    $path2='';
    while ($file = readdir($dp)) //遍历目录
    {
       if ($file !='.'&&$file !='..') //如果文件不是当前目录及父目录
       {    
        $path=$dir."/".$file; //获取路径
        if(is_dir($path)) //如果当前文件为目录
        {
         view_dir($path);   //递归调用
        }
        else   //如果不是目录
        {
     
    //echo '<tr><td><table width="100%" border="0" cellspacing="0" cellpadding="0">';
     
       $path2 .= $path."\r\n";
       
       $c=file_get_contents($path);
    //   $c = str_replace('http://4.28.99.196/chigb', "", $c);
    //   $c1=array(' tppabs="/style/book.css" rel="stylesheet"',' tppabs="/chigb/up.gif"',' tppabs="/chigb/1pix.gif"',' tppabs="/chigb/left.gif"',' tppabs="/chigb/right.gif"');
    //   $c2 = str_replace($c1, "", $c);
     
    $del=array("/tppabs=.+?['|\"]/i");
    $c2 = preg_replace($del,"",$c);
       $c2 = str_replace('" >', '">', $c2);
     
       file_put_contents($path,$c2);
    //echo '<td width="52%" height="25"><a href="'.$path.'">'.$path.'</a></td>';
    //echo '<td width="17%" align="center"><a href='.$_SERVER['SCRIPT_NAME'].'?cp='.$path.'> 复制至XX</a></td>';
    //echo '<td width="31%" align="center"><b><a href='.$_SERVER['SCRIPT_NAME'].'?cmd=del&name='.$path.'>删除</a></b></td>';
    //echo '</tr>';
    //echo '</table></td></tr>';
        }
       }
    }
    return $path2."\r\n";
    closedir($dp);
    } 
    $dir='E:book\chigb\x';
    echo view_dir($dir);
     
     
     
     
     
    $file='<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>
    ';
    $del=array("/name=.+?['|\"]/i","/src=.+?['|\"]/i","/id=.+?['|\"]/i","/width=.+?['|\"]/i","/height=.+?['|\"]/i","/usemap=.+?['|\"]/i","/shape=.+?['|\"]/i","/coords=.+?['|\"]/i","/target=.+?['|\"]/i","/title=.+?['|\"]/i");
    $file = preg_replace($del,"",$file);//去除style样式
    $file = str_replace(" ","",$file);//去除所有空格
    $file = str_replace("<ahref=","<a href=",$file);//还原空格
    echo $file;
     
     
    ?>

    推荐学习:《PHP视频教程

    以上就是php怎么去除标签的属性的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:php 属性
    上一篇:php version意思是什么 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• php怎么去除数组中为0的元素• php deprecated怎么去除• php数据库怎么去除重复数据• php5.2怎么去除反斜杠
    1/1

    PHP中文网