Solved

Find and replace code in HTML files (no jQuery)

Posted on 2013-01-21
6
308 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 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Starting up a Project

724 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