Domanda

Uso estensione bbcode PECL per l'analisi dei tag BBCode .

Qualcuno può mostrarmi un modo per sostituire il testo tra i tag BBCode invece di circostante con tag HTML? Voglio creare un [youtube] tag:

[youtube]w0ffwDYo00Q[/youtube]

La mia configurazione per questo tag è simile al seguente:

$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' => '',
    ),
);

Il problema: il testo tra i tag close_tag (ID Youtube) è necessario due volte (per i tag object e embed) quindi non posso usare <=> come previsto.

Risultato: il markup per l'inclusione del lettore Youtube viene creato correttamente, ma successivamente viene stampato l'ID Youtube:

<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

Qualcuno sa come risolvere questo problema?

Grazie in anticipo!

È stato utile?

Soluzione

Non posso provare adesso, quindi non sono sicuro che funzioni ... Ma forse puoi provare questo:

La documentazione di bbcode_create descrive le chiavi / valori che puoi utilizzare per configurare il tuo tag.
Una di queste chiavi è:

  

content_handling opzionale - Fornisce il file    callback utilizzato per la modifica di   contenuti . Notazione orientata agli oggetti   supportato solo dalla richiamata 0.10.1   prototipo è il nome della stringa (stringa   $ content, stringa $ argomento)

Quindi, cosa succede se si definisce quella proprietà, in modo che sia un collegamento a una funzione che modifica il contenuto ... Modificandolo impostandolo su una stringa vuota, per esempio?

Qualcosa del genere, forse:

$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',
    ),
);

E dichiarando la funzione remove_handler in questo modo:

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

O forse in questo modo:

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

Con un po 'di fortuna, questo potrebbe essere sufficiente per rimuovere il contenuto?


MODIFICA dopo il commento sulla mia proposta precedente


Ciao di nuovo,

Questa volta, ho provato ciò che sto suggerendo e sembra funzionare ;-)

Innanzitutto, è possibile impostare '' sia per open_tag sia per close_tag; in questo modo, il <object> callback sarà responsabile di tutto il lavoro.
Qualcosa del genere, quindi:

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

La funzione di callback dovrebbe apparire così:

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 '';
    }
}

In realtà genera l'intero tag <=>, incluse entrambe le occorrenze dell'ID di YouTube.

E se lo chiami così:

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

Ottieni questo 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)

Quale tipo di qualcosa dovrebbe essere ok ;-)
In realtà, se l'hai appena fatto uscire:

echo $output;

Il video è caricato; si chiama Simon's Cat 'Cat Man do' , tra ;-)


Spero che questo risolva meglio il tuo problema, questa volta :-)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top