[php] BB parser

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Ik ben aan het proberen met het maken van een bb parser voor m'n site, omdat die wie ik nu heb te sloom is (0.045 per parse)

Maar nu ben ik bij het punt van de code tag om de smilies en bb codes er niet in te laten vervangen. Volgens mij hoort het gene wat ik nu heb te werken, maar dat doet het dus niet :(

PHP:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?

class ubb
    {

    var $smilie_find = array(
    '&gt;:)',
    ':)',
    '|:(',
    ":'(",
    ':P',       
    );
    
    var $smilie_replace = array(
    '[img]"../pics/smilies/devil.gif">',
    '<img[/img]',
    '[img]"../pics/smilies/frusty.gif">',
    '<img[/img]',
    '[img]"../pics/smilies/puh2.gif"[/img]',
    );
    
    
    function code($input, $numbers, $code)
        {
        $input = trim($input);
                        
        for($i=0; $i < count($this->smilie_find); $i++) {
            $input = str_replace($this->smilie_replace["$i"], $this->smilie_find["$i"], $input);
            //echo $this->smilie_replace["$i"] . " ---- " . $this->smilie_find["$i"] . "<br>\n";
            }
            
        if($code == 0) $output = $input;    
        elseif($code == 1)
            {
            $input = str_replace("&lt;", "<", $input);
            $input = str_replace("&gt;", ">", $input);
            $input = str_replace("&quot;", '"', $input);
            $input = str_replace("&amp;", "&", $input);
            
            ob_start();
            @highlight_string($input);
            $output = ob_get_contents();
            ob_end_clean();
            }

        $output = str_replace("]", "&#93", $output);
        $output = str_replace("[", "&#91", $output);
        
        return $output;
        }
        
    
    function bbcode($input)
        {           
        $input = nl2br(htmlspecialchars(trim($input)));
                            
        for($i=0; $i < count($this->smilie_find); $i++)
            $input = str_replace($this->smilie_find["$i"], $this->smilie_replace["$i"], $input);
        
        $input = preg_replace('!\\[code\](.+)\\[/code\]!', "Script:<hr>" . $this->code("\\1", 1, 1) . "<hr>", $input); 
        $input = preg_replace('!\\[php\](.+)\\[/php\]!', $this->code("\\1", 1, 1), $input); 
        $input = preg_replace('!\\[no\](.+)\\[/no\]!', $this->code("\\1", 0, 0), $input); 
                
        $input = preg_replace('!\\[b\](.+)\\[/b\]!', "<b>\\1</b>", $input); 
        $input = preg_replace('!\\[u\](.+)\\[/u\]!', "<u>\\1</u>", $input); 
        $input = preg_replace('!\\[i\](.+)\\[/i\]!', "<i>\\1</i>", $input); 
        $input = preg_replace('!\\[s\](.+)\\[/s\]!', "<s>\\1</s>", $input); 
        $input = preg_replace('!\\[li\](.+)\\[/li\]!', "<li>\\1</li>", $input); 
    
        $input = preg_replace('!\\[quote\](.+)\\[/quote\]!', "<blockquote>Quote:<hr>\\1</hr></blockquote>", $input); 
        $input = preg_replace('!\[quote=(.+)\](.+)\\[/quote\]!', "<blockquote>Quote van \\1<hr>\\2</blockquote>", $input); 
        
        $input = preg_replace('!\\[url\](.+)\\[/url\]!', "<a href=\"\\1\">\\1</a>", $input); 
        $input = preg_replace('!\[url=(.+)\](.+)\\[/url\]!', "<a href=\"\\1\">\\2</a>", $input); 
        
        $input = preg_replace('!\[img=(.+)\](.+)\]!', "[img]\"\\1\"[/img]", $input); 
        
        return $input;  
        }
    
    }
    
    
$bb = new ubb;
echo $bb->bbcode($_POST["bericht"]);
    
?>
<!-- ff een formpje om te testen :) -->
<form name="form1" method="post" action="">
  <p>
    <textarea name="bericht" cols="80" rows="8" id="bericht"><? echo htmlspecialchars($_POST["bericht"]); ?></textarea>
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>


hopelijk weten jullie wat ik fout doe...

[ Voor 22% gewijzigd door Verwijderd op 10-01-2003 12:56 ]


Acties:
  • 0 Henk 'm!

Verwijderd

Als je

PHP:
1
2
3
4
$bericht="Jo man :)";

$bb = new ubb;
echo $bb->bbcode($bericht);


doet doet ie het wel. Bij mij tenminste.

Edit3: Maar het ging natuurlijk om de UBB tags.

[ Voor 121% gewijzigd door Verwijderd op 10-01-2003 13:06 ]


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
dat werkt ook wel, maar niet het terug vervangen van de smilies.

probeer maar uit met deze code:
code:
1
2
3
4
5
6
7
8
[url=test.htm]test![/url]

[code]:) [b] niet dik [/b] [/code]

[b]dik[/b] :)


[no]geen smilie :)[/no]


ook het vervangen van [ naar de html code &r91; ofzow werkt niet....

[ Voor 14% gewijzigd door Verwijderd op 10-01-2003 13:06 ]


Acties:
  • 0 Henk 'm!

  • edie
  • Registratie: Februari 2002
  • Laatst online: 19:25
in 1 van de eerste regels in de functie vergang je de smlieys al, zonder rekening te houden met de [no ]...

"In America, consumption equals jobs. In these days, banks aren't lending us the money we need to buy the things we don't need to create the jobs we need to pay back the loans we can't afford." - Stephen Colbert


Acties:
  • 0 Henk 'm!

Verwijderd

eddie19 schreef op 10 januari 2003 @ 13:10:
in 1 van de eerste regels in de functie vergang je de smlieys al, zonder rekening te houden met de [no ]...
Het idee is dat ze door de code functie weer terugvervangen worden.

[ Voor 3% gewijzigd door Verwijderd op 10-01-2003 13:11 ]


Acties:
  • 0 Henk 'm!

Verwijderd

Het probleem is dat de code functie gewoon met als argument \1 aangeroepen wordt. preg_replace vervangt namelijk achteraf de \1 door dat wat hij zoekt, je kan het niet als variabele gebruiken.
Pagina: 1