Thread BBCode URL Teile filtern (8 answers)
Opened by John at 2011-05-05 20:09

pq
 2011-05-06 22:00
#148304 #148304
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
hier das ganze mit CPAN:Parse::BBCode:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use strict;
use warnings;

use Parse::BBCode;
my $p = Parse::BBCode->new({
    tags => {
        youtube => {
            code => sub {
                my ($parser, $attr, $content) = @_;
                my $id = $$content;
                if ($id =~ m{^http://www\.youtube\.(?:de|com)/(?:(?:watch)?\?v=|embed/)(\S*)$}) {
                    $id = $1;
                }
                my $escaped = Parse::BBCode::escape_html($id);
                return qq{<iframe width="400" height="300" }
                    . qq{src="http://www.youtube.com/embed/$escaped" }
                    . qq{frameborder="0" allowfullscreen></iframe>};
            },
            parse => 0,
        },
    },
});
my $bbcode = do { local $/; <DATA> };
my $html = $p->render($bbcode);
print $html;

__DATA__
[youtube][/youtube]
[youtube]1234_Abc[/youtube]
[youtube]http://www.youtube.de/?v=1234_Abc[/youtube]
[youtube]http://www.youtube.de/embed/1234_Abc[/youtube]
[youtube]foo bar bam 123[/youtube]
[youtube]http://www.youtube.com/watch?v=Hg8Fa_EUQqY[/youtube]


Editiert von pq: escape_html($id) hinzugefügt
Last edited: 2011-05-06 22:10:26 +0200 (CEST)
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread BBCode URL Teile filtern