经常扒别人网站文章的坑们;我是指那种批量式采集的压根不看内容的,少不了都会用到删除html标签的函数;这里介绍3种不同用途上的方法;
$str='<div><p>这里是p标签</p><img src="/static/imghwm/default1.png" data-src="" alt=" class="lazy" alt="这里是img标签"><a href="">这里是a标签</a><br></div>';
1:删除全部或者保留指定html标签
php自带的函数strip_tags即可满足要求,
使用方法:strip_tags(string,allow);
string:需要处理的字符串;
allow:需要保留的指定标签,可以写多个;
<?php echo strip_tags($str,'<p><a>'); ?> //输出:<p>这里是p标签</p><a href="">这里是a标签</a>
次函数的优点是简单粗暴,但是缺点也很明显;如果有一大堆标签;而我只是想删除指定的某一个;那要写很多需要保留的标签; 所以有了第二个方法;
2:删除指定的html标签
使用方法:strip_html_tags($tags,$str);
$tags:需要删除的标签(数组格式)
$str:需要处理的字符串;
<?php function strip_html_tags($tags,$str){ $html=array(); foreach ($tags as $tag) { $html[]="/(<(?:\/".$tag."|".$tag.")[^>]*>)/i"; } $data=preg_replace($html, '', $str); } echo strip_html_tags(array('p','img'),$str); ?> //输出<div>这里是p标签<a href="">这里是a标签</a><br></div>;
3:删除标签和标签的内容
使用方法:strip_html_tags($tags,$str);
$tags:需要删除的标签(数组格式)
$str:需要处理的字符串;
<?php function strip_html_tags($tags,$str){ $html=array(); foreach ($tags as $tag) { $html[]='/(<'.$tag.'.*?>[\s|\S]*?<\/'.$tag.'>)/'; } $data=preg_replace($html,'',$str); } echo strip_html_tags(array('a','img'),$str); ?> //输出<div><p>这里是p标签</p><img src="/static/imghwm/default1.png" data-src="" alt=" class="lazy" alt="这里是img标签"><br></div>;
很多网站文章里面会带上网站名和链接,比如白俊遥博客;这个函数就是专治这种; 别拿这个函数采集本站啊;不然保证不打死你;
4:终极函数,删除指定标签;删除或者保留标签内的内容;
使用方法:strip_html_tags($tags,$str,$content);
$tags:需要删除的标签(数组格式)
$str:需要处理的字符串;
$ontent:是否删除标签内的内容 0保留内容 1不保留内容
<?php /** * 删除指定的标签和内容 * @param array $tags 需要删除的标签数组 * @param string $str 数据源 * @param string $content 是否删除标签内的内容 默认为0保留内容 1不保留内容 * @return string */ function strip_html_tags($tags,$str,$content=0){ if($content){ $html=array(); foreach ($tags as $tag) { $html[]='/(<'.$tag.'.*?>[\s|\S]*?<\/'.$tag.'>)/'; } $data=preg_replace($html,'',$str); }else{ $html=array(); foreach ($tags as $tag) { $html[]="/(<(?:\/".$tag."|".$tag.")[^>]*>)/i"; } $data=preg_replace($html, '', $str); } return $data; } echo strip_html_tags(array('a'),$str,1); ?> //输出<div><p>这里是p标签</p><img src="/static/imghwm/default1.png" data-src="" alt=" class="lazy" alt="这里是img标签"><br></div>;
前面扯了那么多;其实最后这个函数才是干货;一口气搞定各种标签删除的疑难杂症不费劲;
别看下面这张截图了;无非带点颜色好看,我主要是拿来凑图当文章封面的;
本文为白俊遥原创文章,转载无需和我联系,但请注明来自白俊遥博客baijunyao.com

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor