PHP preg_match_all replace all IMG content with str_replace

Source: Internet
Author: User

The IMG tag of the collected image contains a lot of JavaScript scripts and useless information. You must replace what you want, such as alt. Let's take a look at the content to be filtered. I just copied it: CopyCode Code: sdfsdfsdf 500) {This. resized = true; this. style. width = 500;} "> sfsdfsdfasdfsadf 500) {This. resized = true; this. style. width = 500;} "> sdfsadf 500) {This. resized = true; this. style. width = 500;} "> sdfsdf

The above should be replaced and formed as follows:Copy codeThe Code is as follows: where src = "http://www.xxx.com/upimg/080330/120d1232295023x0.gif" src = "http://www.xxx.com/upimg/080330/others "This address must be retained, because the image uses the source address.

The general method is: first read the IMG label in the content, then extract the SRC of each IMG label, combine it into its own content, and finally replace it.
Preg_match_all is the function I want. It can create a three-dimensional array of the content matched by the regular expression. You can traverse and search for them and replace them. If you are not familiar with it, please refer to the manual, I will not introduce it here. Function Code:Copy codeThe Code is as follows: function Replace ($ Str)
{
Preg_match_all ("/] +>/ISU", $ STR, $ ARR );
For ($ I = 0, $ J = count ($ arr [0]); $ I <$ J; $ I ++ ){
$ STR = str_replace ($ arr [0] [$ I], "}
Return $ 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.