HEX
Server: nginx/1.24.0
System: Linux webserver 6.8.0-85-generic #85-Ubuntu SMP PREEMPT_DYNAMIC Thu Sep 18 15:26:59 UTC 2025 x86_64
User: wpuser (1002)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: //usr/share/doc/libhtml-parser-perl/examples/htext
#!/usr/bin/perl

# Extract all plain text from an HTML file

use strict;
use warnings;
use Encode ();
use HTML::Parser ();

my %inside;

sub tag {
    my ($tag, $num) = @_;
    $inside{$tag} += $num;
    print " ";    # not for all tags
}

sub text {
    return if $inside{script} || $inside{style};
    print encode('utf8', $_[0]);
}

HTML::Parser->new(
    api_version => 3,
    handlers    => [
        start => [\&tag,  "tagname, '+1'"],
        end   => [\&tag,  "tagname, '-1'"],
        text  => [\&text, "dtext"],
    ],
    marked_sections => 1,
)->parse_file(shift)
    || die "Can't open file: $!\n";