Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: regex
Forum PHP.pl > Forum > PHP
topik53
Witam,
potrzebuje pomocy przy kilku regexach . Nie potrafie tutaj w żaden sposób błysnać.
  1. <td valign="middle" align="right" nowrap="nowrap"><span class="gen">Nick:&nbsp;</span></td>
  2. <td width="100%"><!--<table cellspacing="0" cellpadding="0" border="0"><tr><td>--><b><span class="gen"><a href="profil-1686232.html">programmerr1</a></span></b><!--</td><td>&nbsp;<a href="monety.php"><img src="img/lvl/0.gif" border="0"></a></td></tr></table>--></td>
  3. </tr>
  4. <tr>
  5. <td valign="middle" align="right" nowrap="nowrap"><span class="gen">Dołączył:&nbsp;</span></td>
  6. <td width="100%"><b><span class="gen">Dzisiaj 13:35 (1 dni temu)</span></b></td>
  7. </tr>
  8. <tr>
  9. <td valign="middle" align="right" nowrap="nowrap"><span class="gen">Zalogowany:&nbsp;</span></td>
  10. <td width="100%"><b><span class="gen">Dzisiaj 17:16</span></b></td>
  11. </tr>
  12. <tr>
  13. <td valign="top" align="right" nowrap="nowrap"><span class="gen">Postów:&nbsp;</span></td>
  14. <td valign="top"><span class="gen"><b>7</b> (w tym <b>0</b> postów z linkami)</span><br /><span class="genmed">[0.000% z całości / 7.00 postów dziennie]</span> <br /><span class="genmed"><a href="search.php?search_author=programmerr1" class="genmed">Znajdź wszystkie posty <a href="profil-1686232.html">programmerr1</a></a></span></td>
  15. </tr>
  16. <tr>
  17. <td valign="middle" align="right" nowrap="nowrap"><span class="gen">Diamenty:&nbsp;</span></td>
  18. <td width="100%"><b><span class="gen"><a href="diamenty.php" style="color: #a4a4a4;">0</a></span></b></td>
  19. </tr>
  20. <tr>
  21. <td valign="middle" align="right" nowrap="nowrap"><span class="gen">Poziom konta:&nbsp;</span></td>
  22. <td width="100%"><b><span class="gen"><a href="diamenty.php" style="color: #a4a4a4;">0</a></span></b></td>
  23. </tr>
  24. <tr>
  25. <td valign="top" align="right" nowrap="nowrap"><span class="gen">Pochwały:&nbsp;</span></td>
  26. <td><b><span class="gen">0</span></b></td>
  27. </tr>
  28. <tr>
  29. <td valign="top" align="right" nowrap="nowrap"><span class="gen">Ostrzeżenia:&nbsp;</span></td>
  30. <td valign="top"><b><span class="gen">0</span></b></td>
  31. </tr>

Potrzebuje z tąd wyciągnać :
-zalogowany
-diamenty
-poziom konta
-pochwaly / prosby
-ostrzezenia.
Napisalem juz 2 regi ale nie wiem czy optymalnie.
Prosze o pomoc.
zegarek84
wybierz coś innego:
PHP DOM
phpQuery - biblioteka zwraca elementy drzewa DOM a odpytywać możesz przez selektory CSS jak w jQuery
PHP Simple HTML DOM Parser
topik53
niestety jestem pod tym wzgledem ograniczony, musza byc regexy.
Sinevar
Nie wiem czy o to Ci chodziło...ale ja bym to zrobił na przykład linuxowym sedem:

cat plik.html | sed -n '/Zalogowany\|Diamenty\|Poziom konta\|Pochwały\|Prośby\|Ostrzeżenia/p' | sed 's/\(.*\)<span class="gen">\(.*\):&nbsp;<\/span>\(.*\)/\2/'
topik53
chcialbym cos na zasadzie tego
  1. [/php]preg_match_all('/<b>(.*?)<\/b> /', $get_source, $preg_match); //Wyciąganie liczby postów
  2. $posty = $preg_match[1][0];[php]


ref

ref
Sinevar
Generalnie parsowanie htmla z użyciem regexp to złooooooo. Poczytaj sobie http://stackoverflow.com/questions/1732348...-contained-tags, dość zabawny post smile.gif

Nie znaczy to, że jest to niemożliwe. Dla Twojego przypadku napisałem mały skrypt. Pierwsza funkcja, jako pierwszy parametr pobiera ten Twój html, który zamieściłeś w poście wyżej, drugi parametr to będzie tag tr, a trzeci to tablica opcji do kolejnej funkcji.

  1. function search($text, $tag = 'ol', array $options)
  2. {
  3. static $max0Levels = null;
  4.  
  5. $pattern = "#\<$tag\>(((?:[^<]|\<(?!/?$tag\>)|(?R)))*)\</$tag\>#";
  6. preg_match_all($pattern, $text, $matches);
  7.  
  8. $count = count($matches);
  9. if (null === $max0Levels)
  10. {
  11. $count = @count($matches[1]) ?: 0;
  12. $max0Levels = $count;
  13. }
  14.  
  15. if (1 < $count)
  16. {
  17. for ($i = 0 ; $i < $count ; $i++)
  18. {
  19. if (is_string($matches[1][$i]) && 0 < strlen($matches[1][$i]))
  20. {
  21. if (false !== strstr($matches[1][$i], $options['word']))
  22. {
  23. $max0Levels = null;
  24. echo $options['debug'] ? htmlentities($matches[1][$i]) . '<br/>' : '';
  25. return searchBetween($matches[1][$i], $options['beforeWord'], $options['afterWord'], $options['debug']);
  26. }
  27. }
  28. }
  29. }
  30. $max0Levels = null;
  31. }


Ta druga funkcja wygląda natomiast tak:

  1. function searchBetween($content, $start, $end, $debug = false)
  2. {
  3. // . \ + * ? [ ^ ] $ ( ) { } = ! < > | :
  4. // - which was added in php 5.3, so maybe it's wise to add it anyway as a second parameter
  5. $start = preg_quote($start, '/-');
  6. $end = preg_quote($end, '/-');
  7. $pattern = "/$start(.*)$end/";
  8.  
  9. preg_match($pattern, $content, $matches);
  10.  
  11. echo $debug ? 'pattern: ' . htmlentities($pattern) . str_repeat(' ', 10) . 'value: ' . htmlentities(@$matches[1]) . '<br/>' : '';
  12.  
  13. return isset($matches[1]) ? $matches[1] : $matches;
  14. }


A poniżej parę wywołań, by uzyskać dane, które chciałeś.

  1. $r = search($text, 'tr', array(
  2. 'word' => 'Zalogowany',
  3. 'beforeWord' => '<b><span class="gen">',
  4. 'afterWord' => '</span></b>',
  5. 'debug' => true
  6. ));
  7.  
  8. $r = search($text, 'tr', array(
  9. 'word' => 'Diamenty',
  10. 'beforeWord' => '<a href="diamenty.php" style="color: #a4a4a4;">',
  11. 'afterWord' => '</a>',
  12. 'debug' => true
  13. ));
  14.  
  15. $r = search($text, 'tr', array(
  16. 'word' => 'Poziom konta',
  17. 'beforeWord' => '<a href="diamenty.php" style="color: #a4a4a4;">',
  18. 'afterWord' => '</a>',
  19. 'debug' => true
  20. ));
  21.  
  22. $r = search($text, 'tr', array(
  23. 'word' => 'Pochwały',
  24. 'beforeWord' => '<b><span class="gen">',
  25. 'afterWord' => '</span></b>',
  26. 'debug' => true
  27. ));
  28.  
  29. $r = search($text, 'tr', array(
  30. 'word' => 'Ostrzeżenia',
  31. 'beforeWord' => '<b><span class="gen">',
  32. 'afterWord' => '</span></b>',
  33. 'debug' => true
  34. ));


Pamiętaj by struktura htmla była właściwa, tam w Twoim htmlu brakuje otwierającego tr.
topik53
Dzieki za poswiecenie czasu, dzis postaram sie wszystko przemelic i ogarnać.
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.