PHP uses regular expressions to replace the src content in Img in batches to obtain thumbnails.

Source: Internet
Author: User

PHP uses regular expressions to replace the src content in Img in batches to obtain thumbnails.

This example describes how PHP can obtain thumbnails by replacing the src content in Img in batches based on regular expressions. We will share this with you for your reference. The details are as follows:

Here, PHP uses regular expressions to replace the src content in Img in batches to obtain image path thumbnails.

Many regular expressions on the internet can only get or replace the src content of one img, or can only replace fixed strings. It takes several hours to dynamically Replace the content of multiple images.

/*** Replace the image address with the compressed URL * @ param string $ content * @ param string $ suffix */function get_img_thumb_url ($ content = "", $ suffix = "! C550x260.jpg ") {$ pregRule ="/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Jpg | \. jpeg | \. png | \. gif | \. bmp]) [\ '| \ "]. *? [\/]?> /"; $ Content = preg_replace ($ pregRule, '', $ content); return $ content ;}

Sample Code:

$content = '<a href="#" rel="external nofollow" rel="external nofollow" ></a>'.'<p></p>';$newct = get_img_thumb_url($content);print_r($newct);

Output result:
Copy codeThe Code is as follows: <a href = "#" rel = "external nofollow" rel = "external nofollow"> </a> <p> </p>

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

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.