Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [Regex] - Aiuto con una regex per un bbcode tag in una forum board

    Salve come da titolo vorrei una mano con una modifica al core di un plugin per Wordpress che implementa un forum in Wordpress.

    Il parsing dei bbcode è assegnato a due file: bbtohtml.php e htmltobb.php (di chiara comprensione).

    Questo bbtohtml da me modificato:

    Codice PHP:
    <?php
    /*
    Simple:Press
    bbCode to xhtml parser
    $LastChangedDate: 2011-01-23 10:56:55 -0700 (Sun, 23 Jan 2011) $
    $Rev: 5346 $
    */

    if(preg_match('#' basename(__FILE__) . '#'$_SERVER['PHP_SELF'])) {
        die(
    'Access Denied');
    }

    function 
    sf_BBCode2Html($text$dobr=true)
    {

        
    $text trim($text);

        
    # BBCode [code]
        
    if (!function_exists('bbtohtml_escape')) {
            function 
    bbtohtml_escape($s) {
                global 
    $text;
                
    $text strip_tags($text);
                return 
    '<code>'.htmlspecialchars($s[1]).'</code>';
            }
        }
        
    $text preg_replace_callback('/\[code\](.*?)\[\/code\]/ms'"bbtohtml_escape"$text);

        
    # BBCode to find...
        
    $in = array(      '/\[b\](.*?)\[\/b\]/ms',
                         
    '/\[i\](.*?)\[\/i\]/ms',
                         
    '/\[u\](.*?)\[\/u\]/ms',
                         
    '/\[left\](.*?)\[\/left\]/ms',
                         
    '/\[right\](.*?)\[\/right\]/ms',
                         
    '/\[center\](.*?)\[\/center\]/ms',
                         
    '/\[img\](.*?)\[\/img\]/ms',
                         
    '/\[url\="?(.*?)"?\](.*?)\[\/url\]/is',
                         
    '/\[url\](.*?)\[\/url\]/is',
                         
    '/\[quote\](.*?)\[\/quote\]/ms',
                         
    '/\[list\=(.*?)\](.*?)\[\/list\]/ms',
                         
    '/\[list\](.*?)\[\/list\]/ms',
                         
    '/\[B\](.*?)\[\/B\]/ms',
                         
    '/\[I\](.*?)\[\/I\]/ms',
                         
    '/\[U\](.*?)\[\/U\]/ms',
                         
    '/\[LEFT\](.*?)\[\/LEFT\]/ms',
                         
    '/\[RIGHT\](.*?)\[\/RIGHT\]/ms',
                         
    '/\[CENTER\](.*?)\[\/CENTER\]/ms',
                         
    '/\[IMG\](.*?)\[\/IMG\]/ms',
                         
    '/\[URL\="?(.*?)"?\](.*?)\[\/URL\]/is',
                         
    '/\[QUOTE\](.*?)\[\/QUOTE\]/ms',
                         
    '/\[LIST\=(.*?)\](.*?)\[\/LIST\]/ms',
                         
    '/\[LIST\](.*?)\[\/LIST\]/ms',
                         
    '/\[\*\]\s?(.*?)\n/ms',
                         
    '/\[tex\](.*?)\[\/tex\]/ms',  #Added by Tid: Latex tag
                         
    '/\[TEX\](.*?)\[\/TEX\]/ms'
        
    );
        
    # And replace them by...
        
    $out = array(     '[b]\1[/b]',
                         
    '[i]\1[/i]',
                         
    '<u>\1</u>',
                         
    '<div style="text-align:left">\1</div>',
                         
    '<div style="text-align:right">\1</div>',
                         
    '<div style="text-align:center">\1</div>',
                         
    '[img]\1[/img]',
                         
    '[url="\1"]\2[/url]',
                         
    '\1',
                         
    '<blockquote>\1</blockquote>',
                         
    '[list=1]\2[/list]',
                         
    '<ul>\1[/list]',
                         
    '[b]\1[/b]',
                         
    '[i]\1[/i]',
                         
    '<u>\1</u>',
                         
    '<div style="text-align:left">\1</div>',
                         
    '<div style="text-align:right">\1</div>',
                         
    '<div style="text-align:center">\1</div>',
                         
    '[img]\1[/img]',
                         
    '[url="\1"]\2[/url]',
                         
    '<blockquote>\1</blockquote>',
                         
    '[list=1]\2[/list]',
                         
    '<ul>\1[/list]',
                         
    '[*]\1',
                         
    '$$\1$$',
                         
    '$$\1$$'
        
    );
        
    $text preg_replace($in$out$text);

        
    # special case for nested quotes
        
    $text str_replace('[quote]''<blockquote>'$text);
        
    $text str_replace('[/quote]''</blockquote>'$text);

        
    # paragraphs
        
    if($dobr)
        {
            
    $text str_replace("\r"""$text);

            
    # clean some tags to remain strict
            
    if (!function_exists('bbtohtml_removeBr')) {
                function 
    bbtohtml_removeBr($s) {
                    return 
    str_replace("
    "
    ""$s[0]);
                }
            }

            
    $text preg_replace_callback('/<pre>(.*?)<\/pre>/ms'"bbtohtml_removeBr"$text);
            
    $text preg_replace('/

    <pre>(.*?)<\/pre><\/p>/ms'
    "<pre>\\1</pre>"$text);

            
    $text preg_replace_callback('/<ul>(.*?)<\/ul>/ms'"bbtohtml_removeBr"$text);
            
    $text preg_replace('/

    <ul>(.*?)<\/ul><\/p>/ms'
    "<ul>\\1[/list]"$text);
        }

        return 
    $text;
    }

    ?>
    Che funziona perfettamente con la mia modifica, mettengo i tag [tex][/tex] viene visualizzato correttamente il testo tra i due segni di dollaro. (Poi uno script chiamato Mathjax mi sostituisce la stringa tra $$$$ nella giusta formulazione Latex)

    La mia aggiunta:
    Codice PHP:
    // Nell'array dei bbcode
    '/\[tex\](.*?)\[\/tex\]/ms',  #Added by Tid: Latex tag
     
    '/\[TEX\](.*?)\[\/TEX\]/ms'

    //Nell'array dei BBcode

    '$$\1$$' 
    Il problema ora sorge quando un utente deve modificare il suo messaggio. Mathjax sostituisce l'espressione $$(codice Latex)$$ con una serie di span:

    Codice PHP:
    <span class="MathJax_Preview" style="display: none;"></span> <span class="MathJax" role="textbox" aria-readonly="true" style=""><serie di span annidati> <script id="MathJax-Element-1" type="math/tex">(codice Latex)</script> 
    Io avrei bisogno di recuperare solo il codice latex all'interno degli span per ristamparlo all'atto dell'edit del messaggio tra i tag [tex](codice Latex)[/tex]

    Il codice che si occupa di riparsare l'html in bbcode è questo:
    Codice PHP:
    <?php
    /*
    Simple:Press
    xhtml to bbCode parser
    $LastChangedDate: 2010-03-26 16:38:27 -0700 (Fri, 26 Mar 2010) $
    $Rev: 3818 $
    */

    if(preg_match('#' basename(__FILE__) . '#'$_SERVER['PHP_SELF'])) {
        die(
    'Access Denied');
    }

    function 
    sf_Html2BBCode($text)
    {
        
    $text trim($text);

        
    $text str_replace("\n\n""\n"$text);

        
    $text str_replace ('<div class="sfcode">'"<code>"$text);
        
    $text str_replace ('</div>'"</code>"$text);

        
    # BBCode [code]
        
    if (!function_exists('htmltobb_escape')) {
            function 
    htmltobb_escape($s) {
                global 
    $text;
                return 
    '[code]'.htmlspecialchars_decode($s[1]).'[/code]';
            }
        }
        
    $text preg_replace_callback('/\<code\>(.*?)\<\/code\>/ms'"htmltobb_escape"$text);

        
    # Tags to Find
        
    $htmltags = array(
            
    '/\<b\>(.*?)\<\/b\>/is',
            
    '/\<em\>(.*?)\<\/em\>/is',
            
    '/\<u\>(.*?)\<\/u\>/is',
            
    '/\<ul\>(.*?)\<\/ul\>/is',
            
    '/\<li\>(.*?)\<\/li\>/is',
            
    '/\<img(.*?) src=\"(.*?)\" (.*?)\>/is',
            
    '/\<blockquote\>(.*?)\<\/blockquote\>/is',
            
    '/\<strong\>(.*?)\<\/strong\>/is',
            
    '/\<a href=\"(.*?)\"(.*?)\>(.*?)\<\/a\>/is',
            
    '/\<span class=\'MathJax_Preview\'\>(.*?)\<\/span\>\<script type=\'math\/tex\'\>(.*?)\<\/script\>/is'
            
    );

        
    # Replace with
        
    $bbtags = array(
            
    '[b]$1[/b]',
            
    '[i]$1[/i]',
            
    '[u]$1[/u]',
            
    '[list]$1[/list]',
            
    '[*]$1',
            
    '[img]$2[/img]',
            
    '[quote]$1[/quote]',
            
    '[b]$1[/b]',
            
    '[url=$1]$3[/url]',
            
    '[tex]$2[/tex]'
        
    );

        
    # Replace $htmltags in $text with $bbtags
        
    $text preg_replace ($htmltags$bbtags$text);

        return 
    $text;
    }

    ?>
    Con le mie modifiche già inserite. Io ho provato la regex fuori dal parse del forum e sembra funzionare. Mi sapete dire cosa sbagliO?

  2. #2
    Che cosa non funziona? Ma hai testato a parte la regular expression, tipo con cose del tipo: http://www.spaweditor.com/scripts/regex/ per vedere che effettivamente, dato l'html in output di mathjetc, ti ritorna il risultato aspettato?
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  3. #3
    In effetti non fa nessuna operazione sulla stringa secondo il sito che mi hai dato, anche se provandola in uno script a parte pareva funzionare. Comunque il mio obiettivo è, data la seguente "stringa":

    codice:
    <span class="MathJax_Preview" style="display: none;"></span>
    <span class="MathJax" role="textbox" aria-readonly="true" style="">
    <nobr>
    <span id="MathJax-Span-4" class="math">
    <span style="display: inline-block; position: relative; width: 0.625em; height: 0pt; font-size: 107%;">
    <span style="position: absolute; top: -2.875em; left: 0em; clip: rect(1.835em, 1000em, 3.415em, -0.648em);">
    <span id="MathJax-Span-5" class="mrow">
    <span id="MathJax-Span-6" class="mo" style="font-family: MathJax_Size1; vertical-align: 0em;">
    ∫
    <span style="display: inline-block; overflow: hidden; height: 1px; width: 0.138em;"></span>
    </span>
    </span>
    <span style="display: inline-block; width: 0pt; height: 2.875em;"></span>
    </span>
    </span>
    <span style="border-left: 0em solid; display: inline-block; overflow: hidden; width: 0pt; height: 1.188em; vertical-align: -0.327em;"></span>
    </span>
    </nobr>
    </span>
    <script id="MathJax-Element-1" type="math/tex">
    \int
    </script>
    Devo recuperare tramite una regex questo testo: \int (che non è fisso ma varia). Cioè il testo che si trova all'interno degli ultimi due tag <script>. Poi con pregreplace devo sostituire il tutto con:
    Codice PHP:
    $replace "[TEX]$1[/TEX]"
    $replaced preg_replace($regex,$replace,$string); 

  4. #4
    Originariamente inviato da tidusuper91
    codice:
    <script id="MathJax-Element-1" type="math/tex">
    \int
    </script>
    Non so se può essere la soluzione al tuo caso, in tutti i casi:
    Se la prima riga <script id="MathJax-Element-1" type="math/tex"> , è sempre statica, puoi usare un semplice explode.
    Codice PHP:
    $testo_da_prendere"<script id="MathJax-Element-1" type="math/tex">BLA BLA</script>";
    $ex explode("<script id=\"MathJax-Element-1\" type=\"math/tex\">",$testo_da_prendere);
    $ex explode ("</script>",$ex[1]);
    $testo_preso $ex[0]; //BLA BLA 

  5. #5
    forse come regex:

    codice:
    /<span class="MathJax_Preview".*[\s\S.]*class="mo".*>[\s]*([\S]*)[\s]*<span[\s\S.]*<\/script>/
    dove la sosituzione è

    codice:
    [ T E X T ]$1[ / T E X T ]
    credo almeno
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  6. #6
    Ho modificato di poco la regex
    codice:
    /<span class=\'MathJax_Preview\'>[\s\S.]*<\/span><script .*>([\s\S.]*)<\/script>/is
    perchè la tua recuperava il simbolo di integrale piuttosto che il codice che stava tra i due tag <script></script>
    E sul tuo tester pare funzionare (tra l'altro mi sono accorto che la stringa era diversa).

    Dando questa stringa in pasto al preg_replace:

    Codice PHP:
    <?php
        $string 
    = <<<STR
         <span class='MathJax_Preview'>[img]http://www.tuttingegneri.com/blog/wp-content/plugins/latex/cache/tex_05ac7a592ac15a18edc4cd1610e87ea8.gif[/img]</span><script type='math/tex'>\int</script>
    STR;

    $regex = <<<RX
        /<span class=\'MathJax_Preview\'>[\s\S.]*<\/span><script .*>([\s\S.]*)<\/script>/is
    RX;

    $replace "[tex]$1[/tex]";

    $rep_str preg_replace($regex,$replace,$string);

    echo 
    $rep_str;

    ?>
    Funziona perfettamente. Funziona perfettamente sul tester per le Regex. Non funziona nel forum premendo il tasto edit (il file htmltobb.php l'ho postato sopra). Inizio a pensare che sia una questione di caching. Vi aggiorno!

  7. #7
    Originariamente inviato da tidusuper91
    Ho modificato di poco la regex
    codice:
    /<span class=\'MathJax_Preview\'>[\s\S.]*<\/span><script .*>([\s\S.]*)<\/script>/is
    perchè la tua recuperava il simbolo di integrale piuttosto che il codice che stava tra i due tag <script></script>
    E sul tuo tester pare funzionare (tra l'altro mi sono accorto che la stringa era diversa).

    Dando questa stringa in pasto al preg_replace:

    Codice PHP:
    <?php
        $string 
    = <<<STR
         <span class='MathJax_Preview'>[img]http://www.tuttingegneri.com/blog/wp-content/plugins/latex/cache/tex_05ac7a592ac15a18edc4cd1610e87ea8.gif[/img]</span><script type='math/tex'>\int</script>
    STR;

    $regex = <<<RX
        /<span class=\'MathJax_Preview\'>[\s\S.]*<\/span><script .*>([\s\S.]*)<\/script>/is
    RX;

    $replace "[tex]$1[/tex]";

    $rep_str preg_replace($regex,$replace,$string);

    echo 
    $rep_str;

    ?>
    Funziona perfettamente. Funziona perfettamente sul tester per le Regex. Non funziona nel forum premendo il tasto edit (il file htmltobb.php l'ho postato sopra). Inizio a pensare che sia una questione di caching. Vi aggiorno!

    ah ok pensavo ti servisse il simbolo di integrale cmq i test falli sempre con l'html ESATTO che ti stampa il bbcode, altrimenti non ne vieni fuori
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.