[php] Regexp - BBcode durch Bilder ersetzen

quicksilver

Lt. Junior Grade
Registriert
Jan. 2004
Beiträge
363
Hallo ich habe folgendes vor. Ich habe vor ein bestimmen bbcode:
Code:
[preis=13,80]
Durch Grafiken ersetzen zu lassen. Ich habe schon eine kleine Routine erstellt die Das bewerkstelligt. Aber leider nich in form von bbcode sondern nur im "normalen" text. Ich habe leider regexp nicht so drauf das ich das umsetzen könnte. Kann mir da wer helfen?

PHP:
$trans = array(
		"0" => "<img src=\"0.gif\" alt=\"\">",
		"1" => "<img src=\"1.gif\" alt=\"\">",
		"2" => "<img src=\"2.gif\" alt=\"\">",
		"3" => "<img src=\"3.gif\" alt=\"\">",
		"4" => "<img src=\"4.gif\" alt=\"\">",
		"5" => "<img src=\"5.gif\" alt=\"\">",
		"6" => "<img src=\"6.gif\" alt=\"\">",
		"7" => "<img src=\"7.gif\" alt=\"\">",
		"8" => "<img src=\"8.gif\" alt=\"\">",
		"9" => "<img src=\"9.gif\" alt=\"\">",
		"," => "<img src=\"komma.gif\" alt=\"\">",
		"€" => "<img src=\"euro.gif\" alt=\"\">",
		);
$document = $_GET["preis"];

$text = strtr($document, $trans);

Es soll also so sein das man Per BBCODE [preis=13,80] eingibt und dieser Tag dann durch die images 1.gif, 3.gif, komma.gif, 8.gif,0.gif und abschließend automatisch dahinter das euro.gif setzt.

Wäre echt lieb wenn mir da wer helfen könnte.
 
Naja so ohne weiteres ist es nicht möglich - aber es gibt einen relativ einfachen weg:

PHP:
function preis_zahl_bild($array_matches) {
    $trans = array(
          "0" => "<img src=\"0.gif\" alt=\"\">",
          "1" => "<img src=\"1.gif\" alt=\"\">",
          "2" => "<img src=\"2.gif\" alt=\"\">",
          "3" => "<img src=\"3.gif\" alt=\"\">",
          "4" => "<img src=\"4.gif\" alt=\"\">",
          "5" => "<img src=\"5.gif\" alt=\"\">",
          "6" => "<img src=\"6.gif\" alt=\"\">",
          "7" => "<img src=\"7.gif\" alt=\"\">",
          "8" => "<img src=\"8.gif\" alt=\"\">",
          "9" => "<img src=\"9.gif\" alt=\"\">",
          "," => "<img src=\"komma.gif\" alt=\"\">"
          );
    
    $text = strtr($array_matches[1], $trans); 
    
    return $text . "<img src=\"euro.gif\" alt=\"\">";
}

$str = preg_replace_callback("#\[preis=(.*?)\]#","preis_zahl_bild",$str);
 
Zurück
Oben