Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

16 Experts available now in Live!

Get 1:1 Help Now