Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] preg_replace -> str_replace
Buruniu
post
Post #1





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 30.05.2010

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


Witam.

Mam sobie taki oto kod:
  1. <td class=\"grey\">".($search ? $main->highlight(str_replace("\r\n", "<br />",$rev['comment']), $searchstr) : str_replace("\r\n", "<br />",$rev['comment']))."</td>

Jak widać, używam str_replace do zamiany enterów na <br />, lecz to mi nie wystarcza, bo oprócz tego chciałbym jeszcze dodać zamianę linków na aktywne (klikalne, odnośniki) (bo normalne echo zwraca link w postaci plain tekstu [zwykłego tekstu]).

Chcąc zrobić, to co opisałem wyżej, zamieniam fragment kodu
  1. str_replace("\r\n", "<br />",$rev['comment'])

na
  1. preg_replace("/(http|https|ftp)(:\/\/[[:alnum:](.*?)%\&_=?\/\.-]+)/", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", nl2br($code))

Dzięki czemu otrzymuję preg_replace, które nie dość, ze zamienia mi linki na aktywne to jeszcze entery na <br /> - teoretycznie.

Mój cały kod wygląda teraz tak:
  1. <td class=\"grey\">".($search ? $main->highlight(preg_replace("/(http|https|ftp)(:\/\/[[:alnum:](.*?)%\&_=?\/\.-]+)/", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", nl2br($code)), $searchstr) : preg_replace("/(http|https|ftp)(:\/\/[[:alnum:](.*?)%\&_=?\/\.-]+)/", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", nl2br($code)))."</td>


TEORETYCZNIE powinno działać tak jak chciałem, lecz nie działa.
Czy da się to preg_replace przepisać jakoś żeby to było str_replace (czyli tak jak miałem na samym początku) może wtedy mi zadziała...

Pomoże ktoś? (IMG:style_emoticons/default/smile.gif)

Dzięki,
Pozdrawiam

#EDIT
Jestem debilem. Zapomniałem zamienić $code na $rev['comment']. Teraz już chyba powinno działać.
  1. <td class=\"grey\">".($search ? $main->highlight(preg_replace("/(http|https|ftp)(:\/\/[[:alnum:](.*?)%\&_=?\/\.-]+)/", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", nl2br($code)), $searchstr) : preg_replace("/(http|https|ftp)(:\/\/[[:alnum:](.*?)%\&_=?\/\.-]+)/", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", nl2br($rev['comment'])))."</td>


Ten post edytował Buruniu 26.06.2011, 10:35:48
Go to the top of the page
+Quote Post
mat-bi
post
Post #2





Grupa: Zarejestrowani
Postów: 690
Pomógł: 92
Dołączył: 6.02.2011

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


nl2br to pies?
Go to the top of the page
+Quote Post
Buruniu
post
Post #3





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 30.05.2010

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


Cytat(mat-bi @ 26.06.2011, 11:31:35 ) *
nl2br to pies?

Nie rozumiem. Co masz na myśli?
Go to the top of the page
+Quote Post
mat-bi
post
Post #4





Grupa: Zarejestrowani
Postów: 690
Pomógł: 92
Dołączył: 6.02.2011

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


A przejrzałeś manual chociaż?

Go to the top of the page
+Quote Post
Buruniu
post
Post #5





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 30.05.2010

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


Mam jeszcze pytanie, czy jest jakiś lepszy sposób na zamianę linków na klikalne? Ponieważ poniższy sposób gubi się, gdy w adresie jest spacja lub gdy na końcu adresu bez spacji dodamy jakiś znak...

  1. preg_replace("/(http|https|ftp)(:\/\/[[:alnum:](.*?)%\&_=?\/\.-]+)/", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", nl2br($rev['comment']))

Znalazłem coś takiego:
http://www.totallyphp.co.uk/code/convert_l..._hyperlinks.htm
http://snipplr.com/view/12673/linkify--con...-text-to-links/

Ale jak to tutaj wkomponować?:
  1. <td class=\"grey\">".($search ? $main->highlight(preg_replace("/(http|https|ftp)(:\/\/[[:alnum:](.*?)%\&_=?\/\.-]+)/", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", nl2br($rev['comment'])), $searchstr) : preg_replace("/(http|https|ftp)(:\/\/[[:alnum:](.*?)%\&_=?\/\.-]+)/", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", nl2br($rev['comment'])))."</td>


Ten post edytował Buruniu 27.06.2011, 14:39:14
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: 24.08.2025 - 21:13