Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Wyciagniecie adresow obrazow, z kodu html
ikioloak
post
Post #1





Grupa: Zarejestrowani
Postów: 416
Pomógł: 0
Dołączył: 8.01.2004

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


Napisałem funckje do wyciagania adresow obrazow ze strony htm objetych w tagach <img src=""> oraz w <... background="" >
  1. <?php
  2.  
  3. function getImages($string)
  4. {
  5. $string=substr($string,strpos($string,'<body'));
  6. $pieces=explode(' ',$string);
  7. foreach ($pieces as $val)
  8. {
  9. $pos = strpos($val,'src="');
  10.  
  11. if ($pos === 0)
  12. {
  13.  
  14. $temp=substr(trim($val),5);
  15.  
  16. $temp=substr($temp,0,strlen($temp)-1);
  17. if ((strpos($temp,'.jpg'))||(strpos($temp,'.gif'))||(strpos($temp,'.png')))
  18. $return[]=$temp;
  19. }
  20.  
  21. $pos = strpos($val,'background="');
  22.  
  23. if ($pos === 0)
  24. {
  25.  
  26. $temp=substr(trim($val),12);
  27.  
  28. $temp=substr($temp,0,strlen($temp)-1);
  29. if ((strpos($temp,'.jpg'))||(strpos($temp,'.gif'))||(strpos($temp,'.png')))
  30. $return[]=$temp;
  31. }
  32. }
  33. return $return;
  34. }
  35.  
  36. ?>


Funkcja dziala ale mysle ze mozna rozwiazac to wyrazeniami regularnymi - co moja mocna strona nie jest. Jesli ma ktos pomysl, lub dojzy blad w funkcji to prosze o komentarz.
Go to the top of the page
+Quote Post
crash
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 2 196
Pomógł: 2
Dołączył: 17.01.2004
Skąd: Sosnowiec

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


Moja wersja (ale i tak można pewnie to zoptymalizować):
  1. <?php
  2. function getImages( $string )
  3. {
  4.  $output = array();
  5.  if( preg_match_all( '#<.*(src|background)=('|")([^'"]+)('|").*>#i', $string, $out ) )
  6.  {
  7. $output = $out[ 3 ];
  8.  }
  9.  return $output;
  10. }
  11. ?>
Go to the top of the page
+Quote Post
shpyo
post
Post #3





Grupa: Zarejestrowani
Postów: 574
Pomógł: 2
Dołączył: 13.04.2004
Skąd: Lublin

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


[ot] Skrypt nie ma sensu gdy trafisz na stronę zrobioną w xhtmlu ;] [/ot]
Go to the top of the page
+Quote Post
Radarek
post
Post #4





Grupa: Zarejestrowani
Postów: 188
Pomógł: 0
Dołączył: 23.05.2005

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


Cytat(crash @ 2005-11-22 11:32:04)
Moja wersja (ale i tak można pewnie to zoptymalizować):
  1. <?php
  2. function getImages( $string )
  3. {
  4.  $output = array();
  5.  if( preg_match_all( '#<.*(src|background)=('|")([^'"]+)('|").*>#i', $string, $out ) )
  6.  {
  7. $output = $out[ 3 ];
  8.  }
  9.  return $output;
  10. }
  11. ?>

Zamiast zachlannnego operatora * uzyj *?. Czyli .* zamien na .*?.
Go to the top of the page
+Quote Post

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: 15.09.2025 - 02:11