JDT

 

John Dixon
Technology
Limited

 
Google

Using Perl to Convert Hyperlinks and Filenames to Lowercase


Like a lot of web developers, I am not always that disciplined when it comes to the file naming convention I use and sometimes end up with a situation whereby I have some files that are in lowercase, some that begin with a capital letter, and some that are a bit of a mixture.

One web site I maintain contains about 2000 web pages and has about 20,000 hyperlinks. As you can imagine, I had one of those sinking feelings when I was told that in order to migrate the web site to a new content management system, all the file names and hyperlinks would need to be changed to lowercase.

Whenever I am presented with a problem like this, my instinct is always to write a Perl script using a series of regular expressions to solve the problem. This particular situation was no exception.

Change a string to lowercase

The following regular expression changes all the characters in a string to lowercase. The first part of the regular expression finds a hyperlink, and the second part converts the string.

1. $line =~ s/<a href="(.*?)"/<a href="\L$1"/gs;

Although it looks a bit odd, the '(.*?)' matches any string, and the '$1' is the variable containing that string.

See the series of tutorials beginning with Using Perl and Regular Expressions to Process ASCII Files - Part 1 for more information about how to insert the above line into Perl script.

Change a filename to lowercase

Likewise, changing a filename itself is very simple. The following two lines perform the task quite nicely:

1. use File::Copy;
2. move ("$name", "\L$name");


Author: John Dixon
John Dixon Technology Ltd







Go back to Perl Tutorials home page

Go back to Tutorials home page



Earnings Tracker is John Dixon Technology's FREE open source accounting / bookkeeping software tool.

Aimed at UK contractors and freelancers, Earnings Tracker enables you to perform bookkeeping and accounting tasks, helping you to keep track of your company's earnings and outgoings.

The software is written in PHP and MySQL and is available to use for FREE online, or as a FREE download.

Earnings Tracker can also be used simply as a dividend, corporation tax, or VAT calculator.

Need free accounting software
 


JDT

© 2007-2009 - John Dixon Technology Ltd

Privacy Statement

Terms & Conditions