Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Preg_match dla elementów HTML
Forum PHP.pl > Forum > Przedszkole
vegeta
Witam,

mam taki input: <font class="profilename"><a href="/Saibamen">Saibamen</a></font>

Jak wyciągnąć z niego za pomocą preg_match sam href bez / na początku oraz nazwę, która jest pomiędzy "> a </a></font>
Turson
http://simplehtmldom.sourceforge.net/
lub
  1. $string = '<font class="profilename"><a href="/Saibamen">Saibamen</a></font>';
  2. preg_match('#<font class="profilename"><a href="/(.*)">(.*)</a></font>#is',$string,$matches);
  3. echo '<pre>';
  4. print_r($matches);
  5. echo '</pre>';
vegeta
Nie działa

  1. preg_match( '#<font class="profilename"><a href="/(.*)">(.*)</a></font>#is', $profile_url, $matches );
  2. if( $matches ) {
  3. echo '1';
  4. echo '<pre>';
  5. print_r($matches);
  6. echo '</pre>';
  7. }
  8. else {
  9. echo '0 :(';
  10. }


Wypisuje mi 0. Dodam, że source to file_get_contents z adresu URL

// Edit
Ten kod zadziałał:

  1. preg_match("'<font class=\"profilename\"><a href=\"/(.*?)\">(.*?)</a></font>'si", $profile_url, $match);
  2. if( $match ) echo $match[1].'<br>'. $match[2].'<br>';
Turson
jeżeli w coś $profile_url jeszcze oprócz tego to trzeba zmienić wyrażenie
preg_match( '#(.*)<font class="profilename"><a href="/(.*)">(.*)</a></font>(.*)#is', $profile_url, $matches );
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.