views:

423

answers:

1

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}"&gt;&lt;/param&gt;
                 <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"&gt;&lt;/param&gt;
    <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!

+1  A: 

Hi,

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}"&gt;&lt;/param&gt;
                 <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}"&gt;&lt;/param&gt;
    <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"&gt;&lt;/param&gt;
    <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 :-)

Pascal MARTIN
Thanks for your answer. The content gets removed, but unfortunately this happens before it's rendered into the HTML ;)
marcusj
How :-( too bad :-(
Pascal MARTIN
Just edited my answer, with another proposition, that seems to work, this time ;-)
Pascal MARTIN
Hi, thank you for further investigating :)I think I'll do it that way.
marcusj
You're welcome :-) have fun !
Pascal MARTIN