Regular Get Picture Address link address _ Regular expression

Source: Internet
Author: User

Copy Code code as follows:

reg =/<[img|href][^>]*src\s*=\s* (' | ')? ([^ ' ">]*) \1 ([^>]) *>/ig


Regular Get Picture address

First, the question:

Acquisition of the process encountered a problem: from the database read out of the picture is not normal display, analysis found that the database is the image of the site as a relative path to the root directory storage, picture address such as:/uploads/allimg/090403/ 012f31n9-1.jpg, the original do read the image is http://switch url absolute image acquisition, so the acquisition of the root directory to start the image address collection failed.

Second, the solution:

Analyze HTML code, display picture set tag, write a regular access to the picture address in and add the site to the URL of the site to the absolute address, and then to the following things to the next code (remote image Acquisition to local).

1 Improve the solution to "2) Emergency solutions, as long as the "src=" attribute to start the regular formula, but this way there are imperfections, as long as there are "src=" will be replaced, there are "src=" possibility: JavaScript, the text appears in the "src" and so on. But these are relatively few, plus a picture format to do the regular limit, there will be less chance of being mistakenly replaced.

Third, the implementation code:

Considering the difficulty of the first method is not written for the time being, here is the second kind of emergency solution implementation (implementation of the regular content involved here does not do the explanation and analysis, the regular type of knowledge content please Google or Baidu ^v^):

Copy Code code as follows:

<?php
Test the style, taking into account the regular formula in many cases.
$body = <<<p align=center>1</P>
<p align=center>2</P>
<p align=center>3 </P>
<p align=center>4</P>
<p align=center>5</P>
<p align=center>6</P>
<p align=center>7 </P>
HTML;

The address of the website
$str = ' src=/' http://www.chongwen.com/';
$body =preg_replace ('/src/s*?=/s*?[ /"|/']?/s*?//" (. +?/. (?: jpg|gif|png|bmp)) (/"|/' |/s) *?/s*?/is '", "$str $", $body);
Echo $body;
?>

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.