php regular to achieve the replacement alt method: 1, create a PHP sample file; 2, get the content to be replaced; 3, through "<img.*?src=[\"|\'](. *?) [\"|\']. *? >" can be replaced by the regular implementation.

How to replace alt with php regular?

php regular expressions to extract img alt/title tags and replace them

Sometimes we need to do the necessary processing of the img tags in the rich text editor to meet the needs of the site itself,

For example:

According to the site keywords to set keywords for the alt tag of the img within the page, the following are the rules for extracting and replacing the content of alt/title tags:

1

2

3

4

$title = "Content to be replaced..."

$pattern= "/<img.*?src=[\"|\'](.*?)[\"|\'].*?>/";

$replace = '<img src="$1" alt="'.$title.'"title="'.$title.'">';

$html = preg_replace($patter, $replace, $content);

Related articles

How to convert svg to svg in php?

php svg to jpg method: 1, create a PHP sample file; 2, by "public function svgtojpg(){$image =...}" method to achieve the conversion.

Two basic output methods for php

The two basic output methods of php are "echo" and "print". echo is used to output one or more strings, which can take multiple arguments and have no return value, with the syntax "echo($ str)"; and print is used to output a string that can only take one

How to display a mess of php images?

php image display a mess of solutions: 1, open the corresponding PHP code file; 2, in the page header plus "header ("Content-Type: image/jpg");" code to declare the image type can be.

php can't load js css

php can not load js css solution: 1, check the correct path to load; 2, prohibit the use of absolute path; 3, use relative path to load the file can be.

php how to check user ip

php query user ip method: 1, get user ip by "$_SERVER["REMOTE_ADDR"]" way; 2, get user ip by "($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];" to get the user ip and so on.

How to invert a string by word in php

Inversion methods: 1. use "explode(' ',$str)" statement to convert string to array; 2. use "array_reverse($arr)" statement to invert the array and return the array in the reverse order of elements; 3. use "implode(' ',$arr)" statement to convert the inver

php get maximum value in several numbers

In php, you can use the max() function to get the maximum value among several numbers. The function works by calculating and returning the maximum value among several specified values, with the syntax "max(value1,value2,...) ;".

How to convert php image to base64

php image to base64 method: 1, get an image address; 2, through the "chunk_split(base64_encode($gambar));" method to convert the image to base64 string format can be.

How to merge arrays in PHP

How to merge arrays in PHP? To do this we need to use the array_merge function and the array_merge_recursive function. Here we will introduce these two functions separately.