[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2009-07-08
5
Medium Priority
?
649 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

650 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