Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Wyrazenia regularne - zamiana
Lonas
post 5.10.2006, 08:44:52
Post #1





Grupa: Zarejestrowani
Postów: 576
Pomógł: 14
Dołączył: 9.11.2005

Ostrzeżenie: (20%)
X----


Hej, mam cos takiego :
  1. <?php
  2. $tekst = 'sdafdsaf <a href="3">blad</a>_dwa_<a href="4">blad</a>';
  3.  
  4. preg_match('<a href="[1-9]{1,}">', $tekst, $matches);
  5.  
  6. $link = $matches[0];
  7.  
  8.  
  9. $tekst = str_replace($link,'<a href="index.php"', $tekst); 
  10. // tutaj zamiast a href =index.. poźniej napisze sobie zeby mi ustawialo id=3, nar
    azie chodzi o sama zamiane
  11.  
  12. //$spr = ereg('<a href="[1-9]{1,}">', $tekst);// ewentualne sprawdzenie czy jest a href w tekst
  13. ?>


Celem jest zamiana a href = 3 na a href=index.php?id=3 i tak ze wszystkimi linkami w kodzie.

Kod który wkleilem zamienia mi .. ale tylko 1 a href - jak moge to zapetlic zeby zamienialo mi wszystkie a href


--------------------
Go to the top of the page
+Quote Post
matipl
post 5.10.2006, 09:23:11
Post #2





Grupa: Zarejestrowani
Postów: 58
Pomógł: 0
Dołączył: 6.05.2003
Skąd: Gdańsk

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


Jak zwykle manual się kłania smile.gif Wystarczy, że użyjesz funkcji preg_match_all" title="Zobacz w manualu php" target="_manual


--------------------
Mateusz 'matipl' Kamiński
WWW - http://matipl.pl
Go to the top of the page
+Quote Post
Lonas
post 5.10.2006, 11:13:12
Post #3





Grupa: Zarejestrowani
Postów: 576
Pomógł: 14
Dołączył: 9.11.2005

Ostrzeżenie: (20%)
X----


Po ciezkich zmaganiach doszedlem do tego : moze komus sie przyda :

wyszukuje linki w kodzie i zamienia je na pelne linki smile.gif

  1. <?php
  2.  
  3.  
  4. $tekst = 'sdafdsaf <a href="55">blad</a>_dwa_<a href="456456">blad</a>dwa_<a href="555">blad</a>';
  5. preg_match_all('<a href="[1-9]{1,}">', $tekst, $matches);
  6.  
  7. $ile = COUNT($matches[0]);
  8.  
  9. for ($i=0; $i<$ile; $i++) {
  10.  
  11.  $link_1 = substr($matches[0][$i], 0, 8);
  12.  $link_2 = substr($matches[0][$i],8);
  13.  
  14.  $link_3 = $link_1."pokaz_art.php?id=".$link_2;
  15.  $tekst = str_replace($matches[0][$i],$link_3, $tekst);
  16. }
  17.  
  18. print $tekst;
  19.  
  20.  
  21.  
  22. ?>


--------------------
Go to the top of the page
+Quote Post
bendi
post 5.10.2006, 11:23:41
Post #4





Grupa: Zarejestrowani
Postów: 401
Pomógł: 5
Dołączył: 14.09.2003
Skąd: Wrocław

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


  1. <?php
  2. $tekst = 'sdafdsaf <a href="55">blad</a>_dwa_<a href="456456">blad</a>dwa_<a href="555">blad</a>';
  3. $tekst = preg_replace('/(<a href=")([1-9]{1,}")>/', '1pokaz_art.php?id=2', $tekst );
  4. print $tekst;
  5. ?>


Ten post edytował bendi 5.10.2006, 11:27:12


--------------------
Go to the top of the page
+Quote Post
Lonas
post 6.10.2006, 07:41:24
Post #5





Grupa: Zarejestrowani
Postów: 576
Pomógł: 14
Dołączył: 9.11.2005

Ostrzeżenie: (20%)
X----


$skrot = preg_replace('/(<a href=")([1-9]{1,}">)/', '\\1pokaz_art.php?id=\\2', $skrot );

tak jest dobrze smile.gif

Ten post edytował Lonas 6.10.2006, 07:44:03


--------------------
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 Wersja Lo-Fi Aktualny czas: 25.06.2025 - 09:40