Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 702069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:40:47+00:00 2026-05-14T03:40:47+00:00

I have an interesting problem. I wrote the following perl script to recursively loop

  • 0

I have an interesting problem. I wrote the following perl script to recursively loop through a directory and in all html files for img/script/a tags do the following:

  • Convert the entire url to lowercase
  • Replace spaces and %20 with underscores

The script works great except when an image tag in wrapped with an anchor tag. Is there a way to modify the current script to also be able to manipulate the links for nested tags that are not on separate lines? Basically if I have <a href="..."><img src="..."></a> the script will only change the link in the anchor tag but skip the img tag.


#!/usr/bin/perl

use File::Find;

$input="/var/www/tecnew/";

sub process {
        if (-T and m/.+\.(htm|html)/i) {
                #print "htm/html: $_\n";

                open(FILE,"+<$_") or die "couldn't open file $!\n";
                $out = '';
                while(<FILE>) {
                        $cur_line = $_;
                        if($cur_line =~ m/<a.*>/i) {
                                print "cur_line (unaltered) $cur_line\n";
                                $cur_line =~ /(^.* href=\")(.+?)(\".*$)/i;
                                $beg = $1;
                                $link = html_clean($2);
                                $end = $3;
                                $cur_line = $beg.$link.$end;
                                print "cur_line (altered) $cur_line\n";

                        }
                        if($cur_line =~ m/(<img.*>|<script.*>)/i) {
                                print "cur_line (unaltered) $cur_line\n";
                                $cur_line =~ /(^.* src=\")(.+?)(\".*$)/i;
                                $beg = $1;
                                $link = html_clean($2);
                                $end = $3;
                                $cur_line = $beg.$link.$end;
                                print "cur_line (altered) $cur_line\n";
                        }
                        $out .= $cur_line;

                }
                seek(FILE, 0, 0) or die "can't seek to start of file: $!";
                print FILE $out or die "can't print to file: $1";
                truncate(FILE, tell(FILE)) or die "can't truncate file: $!";
                close(FILE) or die "can't close file: $!";
        } } find(\&process, $input);

sub html_clean {
        my($input_string) = @_;
        $input_string = lc($input_string);
        $input_string =~ s/%20|\s/_/g;
        return $input_string; 
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:40:47+00:00Added an answer on May 14, 2026 at 3:40 am

    Have you considered using a real parser instead of regexps? Regexps are not suitable for parsing HTML! Consider using a parser like HTML::Parser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting problem and would appreciate your thoughts for the best solution.
I have one interesting problem. I must parse mail body (regular expression), get some
I have a very interesting problem on my LinqToSql model. On some of my
So I came across an interesting problem today. We have a WCF web service
He're an interesting problem that looks for the most Pythonic solution. Suppose I have
I have somewhat interesting development situation. The client and deployment server are inside a
I have an interesting situation where I need to deploy an ASP.NET MVC app
I have an interesting query to do and am trying to find the best
This is a bit of a stretch, but I have an interesting (to me)
I have found an interesting issue in windows which allows me to cause the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.