如何为 PECL bbcode 扩展创建 [youtube]-标签?

发布于 2024-07-30 06:02:13 字数 1339 浏览 5 评论 0原文

我使用 PECL bbcode 扩展 来解析 BBCode-Tags。

谁能告诉我一种替换 BBCode 标记之间的文本而不是用 HTML 标记包围文本的方法吗? 我想构建一个 [youtube] 标签:

[youtube]w0ffwDYo00Q[/youtube]

我对此标签的配置如下所示:

$tags = array(
    'youtube' => array(
        'type'     => BBCODE_TYPE_NOARG,
        'open_tag' => 
            '<object width="425" height="350">
                 <param name="movie" value="http://www.youtube.com/v/{CONTENT}"></param>
                 <embed src="http://www.youtube.com/v/{CONTENT}" type="application/x-shockwave-flash" width="425" height="350"></embed>
             </object>',
        'close_tag' => '',
    ),
);

问题:需要 [youtube] 标签之间的文本 (Youtube ID)两次(对于对象和嵌入标签),所以我无法按预期使用 close_tag

结果:正确创建了包含 Youtube 播放器的标记,但之后打印了 Youtube-ID:

<object width="425" height="350">
    <param name="movie" value="http://www.youtube.com/v/w0ffwDYo00Q"></param>
    <embed src="http://www.youtube.com/v/w0ffwDYo00Q" type="application/x-shockwave-flash" width="425" height="350"></embed>
</object>w0ffwDYo00Q

有人知道如何解决此问题吗?

提前致谢!

I use the PECL bbcode extension for parsing BBCode-Tags.

Can anybody show me a way of replacing the text between the BBCode tags instead of surrounding it with HTML tags? I want to build a [youtube] Tag:

[youtube]w0ffwDYo00Q[/youtube]

My configuration for this tag looks like this:

$tags = array(
    'youtube' => array(
        'type'     => BBCODE_TYPE_NOARG,
        'open_tag' => 
            '<object width="425" height="350">
                 <param name="movie" value="http://www.youtube.com/v/{CONTENT}"></param>
                 <embed src="http://www.youtube.com/v/{CONTENT}" type="application/x-shockwave-flash" width="425" height="350"></embed>
             </object>',
        'close_tag' => '',
    ),
);

The Problem: the text between the [youtube] tags (Youtube ID) is needed twice (for object and embed tags) so I cannot use the close_tag as intended.

Result: the markup for inclusion of Youtube player is created correctly but after that the Youtube-ID is printed:

<object width="425" height="350">
    <param name="movie" value="http://www.youtube.com/v/w0ffwDYo00Q"></param>
    <embed src="http://www.youtube.com/v/w0ffwDYo00Q" type="application/x-shockwave-flash" width="425" height="350"></embed>
</object>w0ffwDYo00Q

Anybody knows how to fix this?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2024-08-06 06:02:13

我现在无法测试,所以不确定它是否有效...但也许你可以尝试这个:

bbcode_create 描述了可用于配置代码的键/值。

这些键之一是:

content_handling 可选 - 给出
用于修改的回调
内容。 面向对象的表示法
仅从 0.10.1 回调开始支持
原型是字符串名称(字符串
$内容,字符串$参数)


那么,如果您定义该属性,使其成为修改内容的函数的链接,该怎么办...例如,通过将其设置为空字符串来修改它?

也许是这样的:

$tags = array(
    'youtube' => array(
        'type'     => BBCODE_TYPE_NOARG,
        'open_tag' => 
            '<object width="425" height="350">
                 <param name="movie" value="http://www.youtube.com/v/{CONTENT}"></param>
                 <embed src="http://www.youtube.com/v/{CONTENT}" type="application/x-shockwave-flash" width="425" height="350"></embed>
             </object>',
        'close_tag' => '',
        'content_handling' => 'remove_handler',
    ),
);

并以这种方式声明 remove_handler 函数:

function remove_handler($content, $argument) {
  return '';
}

或者也许那样:

function remove_handler(& $content, $argument) {
  $content = '';
}

如果运气好的话,这可能足以删除内容?


在对我之前的提议发表评论后进行编辑


再次嗨,

这一次,我已经尝试了我的建议,并且似乎有效;-)

首先,你可以为 open_tagclose_tag 设置 '' ; 这样,content_handling 回调将负责所有工作。

像这样,所以:

$tags = array(
    'youtube' => array(
        'type'     => BBCODE_TYPE_NOARG,
        'open_tag' => '',
        'close_tag' => '',
        'content_handling' => 'generate_youtube_tag',
    ),
);

回调函数将如下所示:

function generate_youtube_tag($content, $argument) {
    // TODO some security checks on $content !
    // Here, I've assumed that a youtube id only contains letters and numbers
    // But I don't know it that's always the case
    if (preg_match('/^[\d\w]+$/', $content)) {
        return <<<NEW_CONTENT
<object width="425" height="350">
    <param name="movie" value="http://www.youtube.com/v/{$content}"></param>
    <embed src="http://www.youtube.com/v/{$content}" type="application/x-shockwave-flash" width="425" height="350"></embed>
</object>
NEW_CONTENT;
    }
    else {
        return '';
    }
}

它实际上生成整个 标记,包括 youtube id 的两次出现。

如果你这样称呼它:

$text = '[youtube]w0ffwDYo00Q[/youtube]';
$bbHandler = bbcode_create($tags);
$output = bbcode_parse($bbHandler, $text);
var_dump($output);

你会得到这个输出:

string '<object width="425" height="350">
    <param name="movie" value="http://www.youtube.com/v/w0ffwDYo00Q"></param>
    <embed src="http://www.youtube.com/v/w0ffwDYo00Q" type="application/x-shockwave-flash" width="425" height="350"></embed>
</object>' (length=246)

这看起来应该没问题;-)

实际上,如果你只是输出它:

echo $output;

视频已加载; 它的名字是西蒙的猫'猫人',顺便说一句;-)

Hope this solves your problem better, this time **:-)**

I cannot test right now, so not sure it works... But maybe you can try this :

The documentation of bbcode_create describes the keys/values you can use to configure your tag.

One of these keys is :

content_handling optional - Gives the
callback used for modification of the
content
. Object Oriented Notation
supported only since 0.10.1 callback
prototype is string name(string
$content, string $argument)

So, what if you define that property, so that it is a link to a function modifying the content... Modifying it by setting it to an empty string, for instance ?

Something like this, maybe :

$tags = array(
    'youtube' => array(
        'type'     => BBCODE_TYPE_NOARG,
        'open_tag' => 
            '<object width="425" height="350">
                 <param name="movie" value="http://www.youtube.com/v/{CONTENT}"></param>
                 <embed src="http://www.youtube.com/v/{CONTENT}" type="application/x-shockwave-flash" width="425" height="350"></embed>
             </object>',
        'close_tag' => '',
        'content_handling' => 'remove_handler',
    ),
);

And declaring the remove_handler function this way :

function remove_handler($content, $argument) {
  return '';
}

Or maybe that way :

function remove_handler(& $content, $argument) {
  $content = '';
}

With a bit of luck, this might be enough to remove the content ?


EDIT after the comment about my previous proposition


Hi again,

This time, I've tried what I'm suggesting, and it seems to be working ;-)

First, you can set '' for both open_tag and close_tag ; that way, the content_handling callback will be responsible for all the work.

Something like this, so :

$tags = array(
    'youtube' => array(
        'type'     => BBCODE_TYPE_NOARG,
        'open_tag' => '',
        'close_tag' => '',
        'content_handling' => 'generate_youtube_tag',
    ),
);

The callback function would then look like this :

function generate_youtube_tag($content, $argument) {
    // TODO some security checks on $content !
    // Here, I've assumed that a youtube id only contains letters and numbers
    // But I don't know it that's always the case
    if (preg_match('/^[\d\w]+$/', $content)) {
        return <<<NEW_CONTENT
<object width="425" height="350">
    <param name="movie" value="http://www.youtube.com/v/{$content}"></param>
    <embed src="http://www.youtube.com/v/{$content}" type="application/x-shockwave-flash" width="425" height="350"></embed>
</object>
NEW_CONTENT;
    }
    else {
        return '';
    }
}

It actually generates the whole <object> tag, including both occurences of the youtube's id.

And if you call it like this :

$text = '[youtube]w0ffwDYo00Q[/youtube]';
$bbHandler = bbcode_create($tags);
$output = bbcode_parse($bbHandler, $text);
var_dump($output);

You get this output :

string '<object width="425" height="350">
    <param name="movie" value="http://www.youtube.com/v/w0ffwDYo00Q"></param>
    <embed src="http://www.youtube.com/v/w0ffwDYo00Q" type="application/x-shockwave-flash" width="425" height="350"></embed>
</object>' (length=246)

Which kinda looks like something that should be ok ;-)

Actually, if you just ouput it :

echo $output;

The video is loaded ; it's called Simon's Cat 'Cat Man do', btw ;-)

Hope this solves your problem better, this time **:-)**

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文