Reputation:
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
Upvotes: 1
Views: 123
Reputation: 5072
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">
Upvotes: 2