Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Wyrazenia regularne - zamiana
Lonas
post
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
 
Start new topic
Odpowiedzi
Lonas
post
Post #2





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 (IMG:http://forum.php.pl/style_emoticons/default/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

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: 27.12.2025 - 23:55