Solved

Find and replace code in HTML files (no jQuery)

Posted on 2013-01-21
6
303 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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