Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to dynamically set the form action using jQuery.

827 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