Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] preg_replace - mały problem
Forum PHP.pl > Forum > Przedszkole
OneCode
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.
darko
Nie mam pewności czy dobrze zrozumiałem intencje, ale czy nie chodzi raczej o coś takiego:
  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. }
  7.  
  8.  
  9.  
  10. echo embed_anchor("http://www.php.pl")."<br />";
  11. echo embed_anchor("http://php.pl")."<br />";
  12. echo embed_anchor("www.php.pl")."<br />";


questionmark.gif

Ze sformułowaniem wyrażenia regularnego nie bardzo jest się "o co zaczepić", tzn. nie każdy adres będzie miał www i http, ilość członów w adresie (łańcuch od jednej kropki do następnej) też jest różna.
crackcomm
  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
Fifi209
  1. <?php
  2.  
  3. $text[] = 'www.php.net';
  4. $text[] = 'http://www.wp.pl/';
  5. $text[] = 'http://php.net';
  6.  
  7. foreach ($text as $value) {
  8. echo preg_replace('#(http://)?(.*)#i', '<a href="http://$2">$2</a><br/>', $value);
  9. }
  10.  
  11. ?>


Takie coś ;]
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.