Can php replace the text content of html?
php can replace the text content of html. The replacement method is: 1. Replace through the "preg_match_all($pattern,htmlspecialchars_decode($a)...);" method; 2. Replace through the preg_replace_callback method.
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.
Can php replace the text content of html?
php replaces the content in html
It is known that the following piece of html
$a="<p><img src=\"/upload/store/1/ue/image/1568282125833264.png\" title=\"1568282125833264.png\" alt=\"1.png\"/><img src=\"http://dimg04.c-ctrip.com/images/300q12000000rq2t956CB.jpg\" alt=\"undefined\"/><img src=\"https://dimg04.c-ctrip.com/images/300m12000000sdsca92E2.jpg\"/><img src=\"https://dimg04.c-ctrip.com/images/300w12000000rutmrD6CB.jpg\" alt=\"undefined\"/><img src=\"https://dimg04.c-ctrip.com/images/300u12000000rorex415F.jpg\" alt=\"undefined\"/><img src=\"http://dimg04.c-ctrip.com/images/300k12000000rsyxgBA05.jpg\" alt=\"undefined\"/></p>"
There are URLs containing http, https and local relative paths
Common usage:
$pattern="/<img.*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/"; preg_match_all($pattern,htmlspecialchars_decode($a),$match); if(!empty($match[1])){ print_r($match[1]); }else{ echo "没得"; }
First match all in the loop $match[1]
After loop foreach($match[1] as $val){preg_replace('#src="'.$val.'"/#is', 'src="aaaaa/',$a );}
I think this is quite troublesome
Upgrade usage:
$host="http://mp.csdn.net" $newContent = preg_replace_callback("/<img.*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/", function($m) use($host){ if(strpos($m[1],'http://') || strpos($m[1],'https://')){ return $m[0]; }else{ $img=preg_replace('#src="/#is', 'src="'.$host.'/',$m[0]); return $img; } }, $a);
Although this method is rarely used, it is batch processing The effect is super good!
I personally like this kind of closure function. The code is very readable
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of Can php replace the text content of html?. For more information, please follow other related articles on the PHP Chinese website!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
