Html-PHP regular expressions add connections to img and set connection values

Source: Internet
Author: User
Rich text data in my database is the type of formatted articles that contain html tags, now I want to traverse the data in this rich text and add hyperlinks to all the img, and then set the connection address of the hyperlink to the img Image address directly, the hyperlink contains a pair... rich text data in my database is the type of formatted articles that contain html tags, now I want to traverse the data in this rich text and add hyperlinks to all the img, and then set the connection address of the hyperlink to the img Image address directly, you must set the corresponding class style in the hyperlink. Is there a good alternative?

Reply content:

Rich text data in my database is the type of formatted articles that contain html tags, now I want to traverse the data in this rich text and add hyperlinks to all the img, and then set the connection address of the hyperlink to the img Image address directly, you must set the corresponding class style in the hyperlink. Is there a good alternative?

You can use regular expressions.preg_replace()Function. Replace as follows

// $ Desc your rich text data $ replace = preg_replace ('/<\ s * img \ s + [^>] *? Src \ s * = \ s * ('| \")(.*?) \ 1 [^>] *? \/? \ S *>/I ',' $ 0', $ desc );

You can usephpOfpreg_replace()Replace functions with regular expressions.

In addition to LS expressions. If you only want users to jump to view the large Image/source image when clicking the image, why not use js? You can do this with simple code without having to do it on the server:

$('body').on('click', 'img', function(){    location.href = $(this).attr('src');});

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.