htmanning has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to resize the youtube iframe code so that it works in a mobile browser. For example:
<iframe width="560" height="349" src="http://www.youtube.com/embed/8lG +m40pxw" frameborder="0" allowfullscreen></iframe>
I need to set the width to 300 and adjust the height accordingly. Sometimes the width is 560, other times it might be 480, or 600. I was using the following but it no longer applies because Google changed their code around:
$video =~ s! (<iframe [ ]title="YouTube[ ]video[ ]player" [ ]width=")([0-9]+)(" [ ]height=")([0-9]+)(" ) ! $1 . "300" . $3 . ((300/$2)*$4) . $5 !xeg;
The above no longer works. I'm guessing because there is no longer a Title tag in the YouTube URL. Any help would be appreciated. I also need a way to take the iframe code and grab only the URL contained within it. I'm not sure how to accomplish that. Thanks.

Replies are listed 'Best First'.
Re: Resize video for mobile browser
by kcott (Archbishop) on Aug 15, 2011 at 06:44 UTC

    I note you are matching against attribute="value" throughout and matching a single space between attribute/value pairs. The source HTML could easily contain additional whitespace such as attribute = "value".

    If you're going to do the matching yourself (rather than using one of the tools like those shown above), you need to take this into consideration.

    -- Ken

Re: Resize video for mobile browser
by Anonymous Monk on Aug 15, 2011 at 02:30 UTC

    Any help would be appreciated.

    The sooner you switch to pQuery, HTML::TreeBuilder::LibXML/HTML::TreeBuilder::XPath ... the better off you'll be

    #!/usr/bin/perl -- use strict; use warnings; my $fafafa = <<'__FAFAFA__'; <iframe width="560" height="349" src="http://www.youtube.com/embed/8lG +m40pxw" frameborder="0" allowfullscreen></iframe> __FAFAFA__ print $fafafa; $fafafa =~ s~<iframe([^>]+)>~'<iframe '.Fafafa("$1");~gie; print $fafafa; sub Fafafa { my %kava; my @bool; while( $_[0] =~ m{ \G (?: ( [^'"\s=]+ ) # $1, key \s* = \s* " ( [^"]+)" # $2, val ) | ( [^'"\s=]+ ) # single | \s }gxs ){ no warnings 'uninitialized'; print "1($1)2($2)3($3)\n"; if( $3 ){ push @bool, $3 } elsif( $1 ) { $kava{$1}=$2; } } use DDS; Dump(\%kava); return join ' ', @bool, map { qq'$_="$kava{$_}"' } keys %kava; } __END__
    <iframe width="560" height="349" src="http://www.youtube.com/embed/8lG +m40pxw" frameborder="0" allowfullscreen></iframe> 1()2()3() 1(width)2(560)3() 1()2()3() 1(height)2(349)3() 1()2()3() 1(src)2(http://www.youtube.com/embed/8lGm40pxw)3() 1()2()3() 1(frameborder)2(0)3() 1()2()3() 1()2()3(allowfullscreen) $HASH1 = { frameborder => 0, height => 349, src => 'http://www.youtube.com/embed/8lGm40pxw', width => 560 }; <iframe allowfullscreen width="560" frameborder="0" src="http://www.yo +utube.com/embed/8lGm40pxw" height="349"</iframe>
Re: Resize video for mobile browser
by Mr. Muskrat (Canon) on Aug 15, 2011 at 17:29 UTC