Solved

Replace a Dynamic <IMG> Tag from a MySQL Database Field...

Posted on 2009-07-08
5
636 Views
Last Modified: 2013-12-13
I have a MySQL database (Wordpress ). I need to query the past posts (16,000k+) and replace all IMG tags with another one. Thing is, the original IMG tags aren't fixed. They all point to slightly different URL's. However, the line of code that surrounds it is always the same.

In the line of code below, everything after "thumb" and before the > changes.

Please help.
<img class="photoimg" src="http://www.website.com/thumb/123456/1/photo_123456-name-of-photo.jpg" style="block;" alt="name of photo"></img>

Open in new window

0
Comment
Question by:BlueSquares
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:eejones
ID: 24804577
Give your image an id:

<img id='image1' src="chocolate.gif">

Then use JavaScript to swap the image src:

document.image1.src='vanilla.gif'
0
 

Author Comment

by:BlueSquares
ID: 24804772
@eejones

This sounds like a great solution. Can you hook up the JS code and tell me where to place it?

Also, will this care if each original IMG SRC url is different? Or does it only look for the ID?
0
 
LVL 4

Expert Comment

by:eejones
ID: 24804864
Here is a sort of pseudocode.

begin looping through query result set

{

Build the image url, including the full path and filename, as variable $imagefilename. (Will this image and path always be the same? In other words are you replacing all the images with a single new image?  If so, do this step before you begin this loop.)

Build the new alt text as $alttext

<script type="text/javascript" language="javascript">
document.image1.src='$imagefilename' ;
document.image1.alt='$alttext' ;
</script>

}

end looping through query results

The JavaScript only looks at the image id. It does not look at the original image src at all.


0
 

Accepted Solution

by:
BlueSquares earned 0 total points
ID: 24805981
eejones, I couldn't get it to work. I tried inserting the Javascript in the head and body with no success.

I ended up fixing this myself. I ran the following query on the database:

UPDATE wp_posts SET post_content = replace(post_content,'www.oldwebsite.com','www.mywebsite.com/mynewimage.gif?')

This worked for 2 reasons:
1) All images were being called from a 3rd party server with the same www.oldwebsite.com domain.
2) the ? after the mynewimage.gif ignores any trailing img src parameters in the original IMG SRC. So only my images gets displayed with no errors.

It's a real hack...but I'm happy. 160k rows updated ok. Whew...

Mods...not sure what to do here.
0
 
LVL 4

Expert Comment

by:eejones
ID: 24806200
Glad you were able to solve it.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question