Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> 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 (IMG:http://forum.php.pl/style_emoticons/default/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 (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) mozliwe ze preg_replace albo jeszcze jakas inna funkcja (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
borec
post
Post #2





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

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


po glebszym przemyslunku: chyba popatrze w kod phpBB (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) ale nie obrazilbym sie na odpowiedz :wink:
Go to the top of the page
+Quote Post
BzikOS
post
Post #3





Grupa: Przyjaciele php.pl
Postów: 660
Pomógł: 0
Dołączył: 28.08.2002
Skąd: Starachowice

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


Skoro udało Ci się to zrobić przy pomocy str_replace to znaczy, że nie ma potrzeby używać wyrażeń regularnych. Radziłbym Ci tylko zrobić dwie tablice - jedna zawierająca elementy zamieniane, a druga elementy na jakie ma zamienić i dopiero wrzucenie tych dwóch tablic do str_replace. Przykład:

[php:1:99850d1ae6]<?php
$tempArr1 = Array('[b]', '[u]', '[i']);
$tempArr2 = Array('<b>', '<u>', '<i>');

$str = str_replace( $tempArr1, $tempArr2, $str );
?>[/php:1:99850d1ae6]

Regexpy Ci się przydadzą wtedy, gdy będziesz miał jakieś dane wewnątrz tagu, np. [ url=http://forum.php.pl ]Forum[ /url ]
Go to the top of the page
+Quote Post
borec
post
Post #4





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

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


Cytat
Regexpy Ci się przydadzą wtedy, gdy będziesz miał jakieś dane wewnątrz tagu, np. [ url=http://forum.php.pl ]Forum[ /url ]


wlasnie o to chodzi ze sa (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) popatrz jeszcze raz na kod. z tymi tablicami wlasnie tego nie chcialo mi sie poprawiac (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) to poprosze tylko ten fragment z url w regexp :wink:
Go to the top of the page
+Quote Post
BzikOS
post
Post #5





Grupa: Przyjaciele php.pl
Postów: 660
Pomógł: 0
Dołączył: 28.08.2002
Skąd: Starachowice

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


Aż przysiadłem nad tym i zacząłem skrobać (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) Oto co naskrobałem:

Kod
<?php

$str = "blah [url=http://forum.php.pl]Forum[/url] [b]bold[/b] [i]italic[/i] [img]http://forum.php.pl/templates/subSilver/images/lang_polish/icon_ip.gif[/img] [color=green]zielony[/color]";



$codesArr = Array('url'  => Array('a','href="'),

      'img'  => Array('img','src="'),

      'color'    => Array('span','style="color:'),

      'b'  => 'b',

      'u'  => 'u',

      'i'  => 'i');



foreach( $codesArr as $aKey => $aVal )

    $str = preg_replace_callback( "#[($aKey)[=]?(.*)?](.*?)[/$aKey]#si", 'repl', $str );    



function repl( $matches )

{

    global $codesArr;



    if( is_array( $codesArr[$matches[1]] ) )

    {

  if( !empty( $matches[2] ) )

     $x = '<' . implode( ' ', $codesArr[$matches[1]] ) . $matches[2] . '">' . $matches[3] . '</' . $codesArr[$matches[1]][0] . '>';

  else

     $x = '<' . implode( ' ', $codesArr[$matches[1]] ) . $matches[3] . '">';

    }

    else

    {

  $x = '<' . $codesArr[$matches[1]] . '>' . $matches[3] . '</' . $codesArr[$matches[1]] . '>';

    }



    Return $x;

}





print( $str );

?>


I dziwne.. bo działa (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) Nie wiem jakei jeszcze moga być możliwe BBCody, ale już mi się nie chce myśleć :/

btw. kod php celowo w innych znacznikach (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
Go to the top of the page
+Quote Post
borec
post
Post #6





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

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


8O (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif) aha, dzieki (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 23.08.2025 - 13:15