Php regular expression obtains any attribute in the img Mark (regular expression replacement removes or changes any attribute in the img Mark of the image)

Source: Internet
Author: User
Tags php regular expression
Because there is a project news publishing system, the path of the source image is stored in the database content field (of course there are other text content. When illustrations are in the content, the image path is saved ), however, if you want to use a thumbnail on the front-end for more than one hour, you can solve the problem by having a project news publishing system, the source image path is stored in the content field of the database (of course there are other text content, while the image path is stored in the illustration), but the front-end wants to use thumbnails, after an hour, we can get the following results to solve the problem (the img tag will be used, and any attribute of other tags will naturally be used ):

The code is as follows:


/* Obtain any attribute in the img Mark of the image using regular expressions */

$ Word ='

111 22

Chinese

';
// Retrieve the entire image code
Preg_match ('/] *? Src/s * =/s * (/'| /")(.*?) // 1 [^>] *? //? /S *>/I ', $ word, $ matches );
Echo $ matches [0]; // result:

$ Word ='

111 22

Chinese

';
// Obtain the width
Preg_match ('// I', $ word, $ matches );
Echo $ matches [1];

// Take the height
Preg_match ('// I', $ word, $ matches );
Echo $ matches [1];

// Obtain src
Preg_match ('// I', $ word, $ matches );
Echo $ matches [1];

/* Remove or change any attribute in the img Mark of the image through regular expression replacement *************************** ************************************/
$ Str ='

111 22

Chinese


31313 224344

1212121

';

// Change the src property (here change the original src = "http://files.jb51.net/upload/images/bbb.jpg" to src = "http://files.jb51.net/upload/_thumbs/Images/bbb.jpg ")
Print preg_replace ('/()/I', "/$ {1} _ thumbs/Images // $ {3}", $ str );

/* Change the src attribute,
Here we change the original src = "http://files.jb51.net/upload/images/bbb.jpg" to src = "http://files.jb51.net/upload/_thumbs/Images/bbb.jpg" and discard the width and height
(For example, if you want to display thumbnails on the front-end, but the path of the source image is stored in the database. Why should we discard the width and height ?? You have a thumbnail! Or what will happen to the width and height of the source image ???)
*/
Print preg_replace ('/(/I', "/$ {1}/$ {2} _ thumbs/Images // $ {3}>", $ str );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.