Solved

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

Posted on 2009-07-08
5
630 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now