Home  >  Article  >  Backend Development  >  PHP regularizes any attribute in the img tag (regular replacement removes or changes any attribute in the image img tag)_PHP tutorial

PHP regularizes any attribute in the img tag (regular replacement removes or changes any attribute in the image img tag)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:59:06782browse

Because there is a project news release system, the path of the original image is stored in the database content field (of course there are other text contents. When the illustration is in the content, the image path is stored), but the front desk wants to use thumbnails, and I have been thinking about it for more than an hour. , the following results are obtained, which can solve the problem (you can take the img tag, and you can take any attribute of other tags naturally):

Copy code The code is as follows:

/*Regularly select any attribute in the image img tag*/

$word = '

111 22

Chinese
';
//Get the entire image code
preg_match('/]*?src/s *=/s*(/'|/")(.*?)//1[^>]*?//?/s*>/i',$word,$matches);
echo $matches[0];//Result:

$word = '

111 22

Chinese
';
//Get width
preg_match('//i',$word,$matches);
echo $matches[1];

//Get height
preg_match('//i',$word,$matches);
echo $matches[1];

//Get src
preg_match('//i ',$word,$matches);
echo $matches[1];

/*Regular replacement removes or changes any attribute in the image img tag****************** *************************************************** **/
$str = '

111 22

Chinese

31313 224344

1212121
';

//Change the src attribute (here the original src="http://files.jb51.net/upload/images /bbb.jpg" changed to src="http://files.jb51.net/upload/_thumbs/Images/bbb.jpg")
print preg_replace('/()/i',"/${1}_thumbs/Images//${3 }",$str);

/*Change the src attribute,
Change the original src="http://files.jb51.net/upload/images/bbb.jpg" here to src="http://files.jb51.net/upload/_thumbs/Images/bbb.jpg", and discard the width and height
(for example, you want to display thumbnails in the foreground, but the original image is stored in the database path. Why should you abandon the width and height?? Your thumbnail! What will happen if it is still the width and height of the original image???)
*/
print preg_replace('/(/i',"/${1} /${2}_thumbs/Images/ /${3}>",$str);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328177.htmlTechArticleDue to a project news release system, the database content field stores the path of the original image (and of course other text Content, when the illustrations are in the content, the image path is saved), but the front desk wants to use the abbreviation...
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