Solved

Find and replace code in HTML files (no jQuery)

Posted on 2013-01-21
6
302 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
  • 3
  • 2
6 Comments
 
LVL 52

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 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 52

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

20 Experts available now in Live!

Get 1:1 Help Now