Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] preg_replace - mały problem
OneCode
post
Post #1





Grupa: Zarejestrowani
Postów: 25
Pomógł: 0
Dołączył: 29.09.2009

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


Witam,

Chciałbym Was poprosić o pomoc w zastosowaniu wyrażeń regularnych do zamiany tekstu na link.

Moja sytuacja jest taka, że user ustawia sobie status, w którym może dodać linka.
Przed zapisem do bazy zbędne znaki są usuwane i zostaje sam tekst.

I teraz user może podać 3 różne rodzaje linków:

1. http://www.php.pl
2. http://php.pl
3. www.php.pl

Przy wyświetlaniu wyciągam sobie z bazy statusy i zamieniam je na linki, w ten sposób:

  1. $status = $st['statusContent']
  2. $newstatus = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$1</a>", $status);
  3. $newstatus = preg_replace("/(www.[^\s]+)/", "<a href=\"http://$1\" target=\"_blank\" rel=\"nofollow\">$1</a>", $newstatus);
  4. $newstatus = preg_replace("/(http:\/\/www.[^\s]+)/", "<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$1</a>", $newstatus);
  5.  
  6. echo $newstatus;


Problem polega w tym, że to się krzaczy i wyświetla mi jakieś bzdury.

Będę bardzo wdzięczy za pomoc.

Z góry dziękuję i pozdrawiam,
Bartek.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
crackcomm
post
Post #2





Grupa: Zarejestrowani
Postów: 184
Pomógł: 6
Dołączył: 23.02.2008
Skąd: Katowice

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


  1. function embed_anchor($address) {
  2. if(preg_match("/http/",$address) == 0) {
  3. return "<a href=\"http://".$address."\" target=\"_blank\" rel=\"nofollow\">$address</a>";
  4. }
  5. return "<a href=\"".$address."\" target=\"_blank\" rel=\"nofollow\">$address</a>";
  6. }

  1. echo embed_anchor("jestsobiehttp.pl")."<br />";

I co Ty na to?

Moja popozycja
  1. function embed_anchor($address) {
  2. if(!ereg("^http://",$address)) {
  3. return "<a href=\"http://".$address."\" target=\"_blank\" rel=\"nofollow\">$address</a>";
  4. }
  5. return "<a href=\"".$address."\" target=\"_blank\" rel=\"nofollow\">$address</a>";
  6. }


Pozatym
  1. $status = $st['statusContent']; //już tu miałeś błąd
  2. $newstatus = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$1</a>", $status);
  3. $newstatus = preg_replace("/(www.[^\s]+)/", "<a href=\"http://$1\" target=\"_blank\" rel=\"nofollow\">$1</a>", $newstatus);
  4. $newstatus = preg_replace("/(http:\/\/www.[^\s]+)/", "<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$1</a>", $newstatus);
  5.  
  6. echo $newstatus;

Kod jest bez sensu

Ten post edytował crackcomm 29.11.2009, 04:51:53
Go to the top of the page
+Quote Post

Posty w temacie


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: 11.10.2025 - 14:45