Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Preg_match dla elementów HTML
vegeta
post 12.06.2014, 14:23:02
Post #1





Grupa: Zarejestrowani
Postów: 122
Pomógł: 0
Dołączył: 9.11.2008

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


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>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
Turson
post 12.06.2014, 14:25:33
Post #2





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


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>';
Go to the top of the page
+Quote Post
vegeta
post 12.06.2014, 14:34:16
Post #3





Grupa: Zarejestrowani
Postów: 122
Pomógł: 0
Dołączył: 9.11.2008

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


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>';


Ten post edytował vegeta 12.06.2014, 14:42:27
Go to the top of the page
+Quote Post
Turson
post 12.06.2014, 14:44:56
Post #4





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


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 );
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: 24.07.2025 - 23:00