Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php] wyrażenia regularne - "wybieranie" url'i
kacpero1094
post 31.01.2010, 11:02:29
Post #1





Grupa: Zarejestrowani
Postów: 87
Pomógł: 0
Dołączył: 22.12.2008

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


  1. <?php
  2. $location="http://www.ultimate-guitar.com/tabs/led_zeppelin_tabs.htm";
  3. $site=file_get_contents($location);
  4. preg_match_all('#http://www.ultimate-guitar.com/tabs/(.*?)#si',$site,$out);
  5. $i=0;
  6. while($i<count($out)) {
  7. echo $out[0][$i] . '<br />';
  8. $i++;
  9. }
  10. echo '<br />';
  11. $i=0;
  12. while($i<count($out)) {
  13. echo $out[1][$i] . '<br />';
  14. $i++;
  15. }
  16. echo '<br />';
  17. $i=0;
  18. while($i<count($out)) {
  19. echo $out[2][$i] . '<br />';
  20. $i++;
  21. }
  22. ?>
  23.  
  24.  

Jak pobrać wszystkie adresy nut znajdujących się na tej stronie?
To co zrobiłem nie zwraca zupełnie nic.

Ten post edytował kacpero1094 31.01.2010, 11:03:13


--------------------
kacperkolodziej.pl
Go to the top of the page
+Quote Post
matiit
post 31.01.2010, 11:14:20
Post #2





Grupa: Zarejestrowani
Postów: 365
Pomógł: 70
Dołączył: 5.04.2009

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


Pierwsze co rzuca się w oczy... musisz excapować kropkę...tak to uznaje ją za każdy (oprócz znaku nowej lini) znak...może to stwarzać problemy.
http://pl.wikibooks.org/wiki/PHP/Podstawy_...żeń_regularnych

Ten post edytował matiit 31.01.2010, 11:14:47
Go to the top of the page
+Quote Post
l3l0
post 31.01.2010, 12:34:30
Post #3





Grupa: Zarejestrowani
Postów: 94
Pomógł: 16
Dołączył: 24.07.2009
Skąd: Gdańsk

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


Możesz użyć takich klas

  1. /**
  2.  *
  3.  * @package
  4.  * @subpackage
  5.  * @author Leszek Prabucki <leszek.prabucki@gmail.com>
  6.  * @version SVN: $Id$
  7.  */
  8. interface l3NoteDecorator
  9. {
  10. public function getRow($rowText);
  11. }
  12.  
  13. class l3NoteHtmlView implements l3NoteDecorator
  14. {
  15. public function getRow($rowText)
  16. {
  17. return $rowText . "<br/>\n";
  18. }
  19. }
  20.  
  21.  
  22. class l3NoteParser
  23. {
  24. private $location = null;
  25.  
  26. /**
  27.   * Pared array with all links
  28.   *
  29.   * @var string
  30.   */
  31. protected $parsedArray = array();
  32.  
  33. /**
  34.   *
  35.   *
  36.   * @author Leszek Prabucki <leszek.prabucki@gmail.com>
  37.   */
  38. public function __construct($location)
  39. {
  40. $this->location = $location;
  41. }
  42.  
  43. public function parse()
  44. {
  45. $parsedArray = array();
  46. $site = file_get_contents($this->location);
  47.  
  48. preg_match_all('#href="(/tabs/l/.*?\.htm)"#', $site, $parsedArray);
  49.  
  50. $this->parsedArray = isset($parsedArray[1]) ? $parsedArray[1] : array() ;
  51.  
  52. return $this;
  53. }
  54.  
  55. public function show(l3NoteDecorator $decorator)
  56. {
  57. $links = '';
  58.  
  59. foreach ($this->parsedArray as $key => $link)
  60. {
  61. $links .= $decorator->getRow('http://www.ultimate-guitar.com' . $link);
  62. }
  63.  
  64. return $links;
  65. }
  66. }
  67.  
  68. $note = new l3NoteParser('http://www.ultimate-guitar.com/tabs/led_zeppelin_tabs.htm');
  69.  
  70. echo $note->parse()->show(new l3NoteHtmlView());
  71.  
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: 19.07.2025 - 07:47