Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [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
darko
post
Post #2





Grupa: Zarejestrowani
Postów: 2 885
Pomógł: 463
Dołączył: 3.10.2009
Skąd: Wrocław

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


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.

Ten post edytował darko 29.11.2009, 04:27:49


--------------------
Nie pomagam na pw, tylko forum.
Go to the top of the page
+Quote Post
crackcomm
post
Post #3





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


--------------------
Sygnaturka comes here.
Go to the top of the page
+Quote Post
Fifi209
post
Post #4





Grupa: Zarejestrowani
Postów: 4 655
Pomógł: 556
Dołączył: 17.03.2009
Skąd: Katowice

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


  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ś ;]


--------------------
Zainteresowania: C#, PHP, JS, SQL, AJAX, XML, C dla AVR
Chętnie pomogę, lecz zanim napiszesz: Wujek Google , Manual PHP
Go to the top of the page
+Quote Post

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: 19.08.2025 - 21:37