Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> str_replace => preg_match
borec
post
Post #1





Grupa: Zarejestrowani
Postów: 260
Pomógł: 0
Dołączył: 18.07.2003
Skąd: Tarnów

Ostrzeżenie: (0%)
-----


jako ze jestem zielony w wyrazeniach regularnych a nie udalo mi sie znalezc przyjaznego kursu prosze o pomoc z poprawa funkcji podmieniajacej znaczniki (cos w stylu bbCode) w stringu. zrobilem to na str_replace (z reszta i tak beznadziejnie ale nie chcalo mi sie poprawiac :wink:) i chcialbym to zastapic uzyciem preg_match. z gory tnx za pomoc smile.gif

[php:1:52d8ccad3a]<?php
function bbCode ($text) { // bbCode()
$text = nl2br($text);

// usuwa puste znaczniki
$text = str_replace("[ b ][/ b ]", "", $text);
// musialem dodac space zeby forum nie parsowalo
$text = str_replace("[ i ][/ i ]", "", $text);
$text = str_replace("[ u ][/ u ]", "", $text);
$text = str_replace("[img][/img]", "", $text);
$text = str_replace("", "", $text);

// zamienia znaczniki na html
$text = str_replace("[ b ]", "<b>", $text);
$text = str_replace("[/ b ]", "</b>", $text);
$text = str_replace("[ i ]", "<i>", $text);
$text = str_replace("[/ i ]", "</i>", $text);
$text = str_replace("[ u ]", "<u>", $text);
$text = str_replace("[/ u ]", "</u>", $text);
$text = str_replace("[img]", '<img src="', $text);
$text = str_replace("[/img]", '" alt="" align="left">', $text);
$text = str_replace("[url=", '<a href="', $text);
$text = str_replace("[/url]", "</a>", $text);

// zamiana kolorow
$text = str_replace("[color=", '<span style="color: ', $text);
$text = str_replace("[/color]", "</span>", $text);

// zmiana wielkosci textu
$text = str_replace("[size=", '<span style="font-size: ', $text);
$text = str_replace("[/size]", "</span>", $text);

$text = str_replace("]", '">', $text);
/* to robi najwiecej zamieszania, np.
gdy w stringu (konkretnie tresci newsa)
wystepuja nawiasy kwadratowe nie zwiazane ze znacznikami
*/

return $text;
}
?>[/php:1:52d8ccad3a]

update: chyba preg_match tongue.gif mozliwe ze preg_replace albo jeszcze jakas inna funkcja smile.gif


--------------------
Gentoo Linux 64bit / PHP 5.2 / MySQL 5.1
-> Administracja serwerami Linux i FreeBSD
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 18:24