?
Solved

Find and replace code in HTML files (no jQuery)

Posted on 2013-01-21
6
Medium Priority
?
310 Views
Last Modified: 2013-10-09
I have 3000+ html files in a folder. Is it possible to use any scripting to find inside all html files, any image tag that has src="XXX_logo.png" and add text ending with " logo" to the alt attribute which already contains a value?

I can't use jQuery.

EG:
BEFORE:
<div class="copyright">
<div class="floatleft"><img src="support_files/organisation_logo.png" alt="The organisation" width="104" height="48" class="floatleft"/></div>
<div class="footer-statements"><div class="floatright"><img src="support_files/partner_logo.png" alt="Organisation partner name" width="273" height="48" /></div>
<div class="copyright-lines"><!-- metadata:content-rights--></div>
</div>

Open in new window



AFTER (desired result):
<div class="copyright">
<div class="floatleft"><img src="support_files/organisation_logo.png" alt="The organisation logo" width="104" height="48" class="floatleft"/></div>
<div class="footer-statements"><div class="floatright"><img src="support_files/partner_logo.png" alt="Organisation partner name logo" width="273" height="48" /></div>
<div class="copyright-lines"><!-- metadata:content-rights--></div>
</div>

Open in new window

0
Comment
Question by:jeremyll
[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
6 Comments
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 38804294
If you use jquery, you would still have to update 3000 pages.  Your dreamweaver can do the find and replace.  You can also do a find and replace in windows or mac automator.   Do you actually have 3000 hard coded html pages or 3000 pages that are really just a handful generated by your database?
0
 

Author Comment

by:jeremyll
ID: 38804366
There's 3000 hard coded HTML pages.

I don't think you can do a find and replace with dreamweaver.

Each HTML page has three images, but I only want to append " logo" to the end of the text in the alt attribute for just two images.
0
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 total points
ID: 38805772
You can do a find and replace with dreamweaver.  Anything from very simple to using reg ex.  You can find and replace by selected text, entire document open, all open files, all files in a "site" or all files in a folder.  As long as you have a pattern you can do it.   I have never tried it with 3000 pages with dreamweaver.  

If you have a javascript or include file in one spot and want to use jquery you would use

$(document).ready(function() {
$('.copyright .floatleft img').attr('alt', 'The organisation logo');
$('.footer-statements img').attr('alt', 'Organisation partner name logo');
 });
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 38806362
You can write a application that

1.Gets all the files in the folders
2. Open each file, use regex to search for the string and then perform the task as required
3. Save back the file :)


This utility can me easily written in C# or Vb.Net and will work like a charm :)

Hope this will give you some idea to solve ur problem
0
 

Author Comment

by:jeremyll
ID: 38807794
@padas - 'The organisation' is meant to be the variable, so I'm not replacing text with 'The organisation logo'. It should be '{variable name} logo'

@AmanBhullar - Unfortunately, don't know c# or vb.net well enough to write the script.

Someone has come up with perl script which I don't know either. :-(

perl -pi -e 'if(/<img src=\".*_logo.png/){s/(.*alt=\"[^\"]*)\"(.*)/$1 logo\"$2/g}' *.html

Looks logical though..
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 38807833
I'm sorry, my eyes read one thing and brain processed another.   Using jquery could do the same thing searching for any image tag that has src="XXX_logo.png.  For me personally, it is easier then regex which is what the perl script is doing.  You can use the same regex commands in dreamweaver by the way.    Also if the variable is the text, jquery can simply append the word logo instad of replacing as I did.  

If this is a one time permanent thing you will not need to do again, I would go with the find and replace and be done with it.  But the js option is non destructive.  With 3000 pages, something is bound to be different then what you expected.

If you can get that perl script to work once, I would use that.
0

Featured Post

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project
Suggested Courses

770 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