473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Search and replace all image files in HTML document

Hey there;

I'm working on creating a Wordpress plugin, but it turns out that my Regular Expressions-fu is lacking.

How can you replace all image files (jpg, gif, jpeg)?

I'm thinking a function with the following

- Receive HTML in a string

- Only replace image urls starting with http://example.com

- Replace the domain with http://example2.com

- Return HTML with image URLs replaced.


... But I can't for the life of me create a well-formed piece of code that does this for me.

Any ideas?
Oct 2 '10 #1
1 2431
kovik
1,044 Expert 1GB
Well, I'm assuming by "image files", you mean <img> tags. Basically, you want to search for those tags and make your replacement. Since you've shown that the example that you want to replace is http://example.com to http://example2.com, I can give you a nice little snippet to get you started.

Expand|Select|Wrap|Line Numbers
  1. function replace_images($text) {
  2.   /** Regex Parts:
  3.    * \1 is the img tag up to the starting quotation mark
  4.    *    around the file name
  5.    * \2 is the quotation mark
  6.    * \3 is the file name without the domain name
  7.    * \4 is the img tag after the ending quotation mark
  8.    *    around the file name
  9.    */
  10.   $current_domain = 'http://example.com/';
  11.   $target_domain= 'http://example2.com/';
  12.   $img_tag_regex = "(<img[^>]+src=)([\"'])$current_domain([^\2]+?)\2([^>]*>)";
  13.   return preg_replace("~$img_tag_regex~s", "\\1\\2$target_domain\\3\\2\\4", $text);
  14. }
Untested, btw. Since you are doing this in WordPress, you'll probably want to set thus function up as a filter.
Oct 4 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
1
by: Zenobia | last post by:
Hello I want a search & replace text in source code for several files in several directories. It would seem that both Dreamweaver MX 6 and GoLive 6 offer this feature but not for .aspx file...
0
by: purespam | last post by:
How can I dynamically add file links to a html document? I need to be able to click an image icon and the "Open file" box should pop up where I can select a document from my harddisk. The...
1
by: mike c | last post by:
I have a search app that searches local HTML files for a specified term. I then display the pages that contain the term. I would like to highlight the search term within the HTML when it is...
10
by: Viken Karaguesian | last post by:
Hello everyone, Me again. Trying to learn some more :>) I hope I got the terminology right. How does a browser parse (correct term?) an HTML document. I'm sure that every browser does it a...
2
by: Jan | last post by:
Hello! I am looking for a way to do a search&replace in ASCII-Files by a vb.net 2005 programm. Of coarse I can open the files, loop to every line, make a replace, and save the line. But I wonder...
3
by: Andre | last post by:
I'm writing a web-app which needs some javascript (my own) to be executed locally. This app can load local pages or remote ones; however I have a concern about potentially malicious code in...
5
by: ivowel | last post by:
dear css experts: I have some TIFF images of text articles. I would love to be able to use the "Find" feature in my browser to a particular word in the text, and to have hrefs in the document...
1
by: Josiwe | last post by:
I have a search program that returns an HTML string which I display to the user. I want to highlight the search terms. However a simple search and replace on the HTML causes problems. If the...
3
by: Mark | last post by:
Hello This might be an odd question. I have an Excel VBA program that searches through a folder for any .htm or .html file, searches the file for any 'hrefs' and prints them to the spreadsheet....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.