1

This is a chat program. I managed to separate the Images from most of the text, but it leaves it embedded in a string. There is no telling where the URL will be located in the string, and if typing is before it or after it, it appears in the string! I need it to separate and place only the image regex URL into the s/$image//.

I have tried while loops, foreach loops and crashed the whole system with a for loop! I do get the image in place but only if I leave a whole blank line for it. Same thing with the webpage....

      if (($searchhttp = m/^http/sig) 
      && ($search_image = m/(.jpg|.jpeg|.gif|.png)/ig)) {
    @jpgimage = @_;
    $jpgimage = $jpgimage[0];

   $jpgimage =~ grep(/(^https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6}) ([\/\w \.-]*)*\/?(?:.jpg|.jpeg|.gif|png)$/sig);

           $image = substr($jpgimage, 0);
           ($image) = split(/\s+/, $jpgimage);
                    chomp($image);

   $filter =~ s/$image/<img src ='$image' align ='left'>/; 

    print $image.'<BR>';
   #print $jpgimage.'<BR>';
  }

If I leave it on just one line, it works... If I type before it or after it it does not. it includes the whole string in the a href, or the img src.

I need to find a way to take it out of the string

Example...

It takes the whole text from that line and places it in the right brackets, just one long string... "testing if this works http://172.31.4.253/images/joe.jpg" "https://www.perltutorial.org lets try this"

I have spent a month on this... and the out come with this code is the best I've gotten!

There could be and most likely be more then one image.

This is the out comes after I paste 5 pictures, one with the word Test in front, and these 4 are placed in the img src...

http://172.31.4.253/images/joe.jpg
https://www.perltutorial.org/wp-content/uploads/2012/11/Perl-Tutorial.jpg
http://172.31.4.253/images/joe.jpg
https://www.perltutorial.org/wp-content/uploads/2012/11/Perl-Tutorial.jpg
2
  • This code is an unsalvagable mess. What is your intention? Do you just need something that works? Or do you want to learn how to code that yourself? Commented Sep 28, 2019 at 13:53
  • Surely I need something that works, and I have done this in the past about 15 years ago, I just can not remember how I did this the last time. I need to break up that string, and place only the image URL into the s/$image/<img src=$image>/ That is the closes I could get now. Commented Sep 28, 2019 at 20:40

1 Answer 1

2

URL parsing and handling is not trivial. It's very easy to get it wrong, thus it should be left to a battle tested module if possible. Consider this code.

use URI;
use URL::Search qw(extract_urls);

my $webpage = join "", <DATA>; # wherever your data comes from

for my $url (extract_urls $webpage) 
{
    my $url_object    = URI->new( $url );
    my $host_ok       = $url_object->host =~ /\.(com|net|jp|org|uk)$/i;
    my $is_image      = $url_object->path =~ /\.(jpg|jpeg|gif|png)$/i;
    my $save_url      = $url_object->canonical;
    my $regex_for_url = quotemeta( $url );

    $webpage =~ s/$regex_for_url/<img src="$save_url">/g
      if $host_ok && $is_image;
}

print $webpage;
__DATA__
https://docs.perl6.org
https://github.xxx/foo.gif
https://docs.perl6.org/camelia.png
https://docs.perl6.org/camelia.gif

Output

https://docs.perl6.org
https://github.xxx/foo.gif
<img src="https://docs.perl6.org/camelia.png">
<img src="https://docs.perl6.org/camelia.gif">
Sign up to request clarification or add additional context in comments.

1 Comment

That code does nothing on this program. I know there is a way and I am on the right track. I at least get the picture from the code, it only messes up if I type in front of it. The message is seen through a filter, The img src is not printed, it is created on the fly through the filter. The only thing that is printed is the text, and the URL. I did this back in the late 90s and early 2000s, and I did not use any module. I just forgot how I did it for reasons beyond my control. I am rewriting a program, I lost long ago when we did hard coding! Thank you for your input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.