JDT |
John Dixon |
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 lowercaseThe 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 lowercaseLikewise, changing a filename itself is very simple. The following two lines perform the task quite nicely: 1. use File::Copy; Author: John Dixon Go back to Perl Tutorials home page Go back to Tutorials home page
|
|
|||||
|
© 2007-2009 - John Dixon Technology Ltd |
|||||