Perlについての質問箱 49箱目
■ このスレッドは過去ログ倉庫に格納されています
0586デフォルトの名無しさん
2011/12/20(火) 13:06:13.16理解できなかった理由は「長すぎたから」では無かろ。
#!/usr/bin/perl -w
use strict; use CGI qw(escapeHTML);
my $str = <<'EOH';
あああ<span title="FONT">FONT</span>www
<span title="www
www">あああaaaFONTwww</span
>aaa<span>FONT<span title="aaaあああaaa">wwwあああwww
</span>FONT</span>
EOH
print 'あああ = ', mark(\$str => 'あああ', with => '#00ff00'), "\n";
print mark($str => 'FONT', with => '#0000ff');
sub mark {
my($str, $target, %css) = @_;
With: {
defined $css{'with'} or last With;
local $_ = delete $css{'with'};
/^#/ and $css{'background-color'} = $_, last With;
}
$css{'font-weight'} ||= 'bold';
my $css = escapeHTML(join ' ', map "$_:$css{$_};", keys %css);
# http://www.din.or.jp/~ohzaki/regex.htm#ReplaceOutside
my $count = (ref $str ? ${$str} : $str) =~
s{ ((?:\G|>)[^<]*?) (\Q$target\E) } {$1<span style="$css">$2</span>}gx;
ref $str ? $count : $str;
}
■ このスレッドは過去ログ倉庫に格納されています