Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: wyrażenie regularne
Forum PHP.pl > Forum > PHP
nowyyy31
Witam

mam mały problem chcę z strony(jest to katalog firm) kartuzy.info/index.php?m=1&prc=0 wyciągnąć dane takie jak
nazwa firmy
tel
adres

tylko jest jeden mały problem nie każda firma ma podany numer tel, i jak niema jakaś firma numeru telefonu podane to przepisuje z niższej firmy
  1. <?php
  2.  
  3. function filtr($text){
  4.   $text = str_replace ('<h1>', '', $text);
  5.   $text = str_replace ('<div class="tel">', '', $text);
  6.   $text = str_replace ('<div class="adres">', '', $text);
  7.   $text = str_replace ('<div class="adres_name">' , '', $text);
  8.   $text = str_replace ('</div>', '', $text);
  9.   $text = str_replace ('</h1>', '', $text);
  10.  
  11.   return $text;
  12. }
  13.  
  14. function tel($page)
  15.     {
  16. preg_match_all('/<div class="adres_name">tel:</div><div class="tel">.*?</div>/s', $page, $m);
  17. if($m == ''){
  18.     $m ="brak tel";
  19. }
  20. foreach($m AS $key => $value)
  21.    {
  22.        $tel = $value;
  23.    }
  24.    
  25.        $GLOBALS["tel"] = $tel;
  26.     }
  27.  
  28. function adres($page)
  29.     {  
  30. preg_match_all('/<div class="adres">.*?</div>/s', $page, $m);
  31.  
  32.    foreach($m AS $key => $value)
  33.    {
  34.        $adres = $value;
  35.    }  
  36.  
  37.    $GLOBALS["adres"] = $adres;
  38.  
  39.     }
  40.  
  41. function kartuzy(){
  42. global   $tel, $adres;
  43.  
  44. $page = implode ('', file ('http://kartuzy.info/index.php?m=1&prc=0'));
  45.  
  46.  
  47. preg_match_all('/<h1>.*?</h1>/s', $page, $m);
  48.  
  49.    foreach($m AS $key => $value)
  50.    {
  51.        $nazwa = $value;
  52.    }
  53.    
  54.  
  55. tel($page);  
  56. adres($page);    
  57.  
  58. $x = count($nazwa);
  59.    $i = 0;
  60.    while($i < $x){
  61.       $a = filtr($tel[$i]);
  62.       $b = filtr($adres[$i]);
  63.       $c = filtr($nazwa[$i]);
  64.       $return = "$return  
  65. <tr height=19 style='height:14.25pt'>
  66.  <td height=19 width=72 style='height:14.25pt;width:54pt'>$c</td>
  67.  <td width=72 style='width:54pt'>$a</td>
  68.  <td width=72 style='width:54pt'>$b</td>
  69. </tr>";
  70.      
  71.      $i++;
  72.   }
  73.  
  74.   return $return;
  75. }
  76.     echo "<table border=0 cellpadding=0 cellspacing=0 width=216 style='border-collapse:
  77. collapse;table-layout:fixed;width:162pt'>
  78. <col width=72 span=3 style='width:500pt'>";
  79.   echo kartuzy();
  80.   echo "</table>";
  81. /*$file = "baza.txt";
  82.  
  83. for ($i = 0; $i < 1; $i++){
  84. $dane = kartuzy($i);
  85. $fp = fopen($file, "a");
  86. flock($fp, 2);
  87. fwrite($fp, $dane);
  88. flock($fp, 3);
  89. }
  90. fclose ($fp);
  91.  */
  92. ?>


jak stworzyć warunek jeżeli niema numeru tel, żeby pokazał się tekst "Brak tel"

seth-kk
wyluskac tabele > explode po <tr> > explode po <td> > wyczyscic dane
nowyyy31
czyli co masz na myśli bo nie rozumiem...

próbowałem dodać warunki typu

  1. <?php
  2. if ($a=='') echo('Brak tel');
  3. ?>


ale nie działa...
erix
Powiem tak - zainteresuj się PHPQuery, będziesz mógł dużo dokładniej wyciągać dane.
aart3k
Podejrzyj sobie po prostu var_dump co jest w tej tablicy kiedy nie ma telefonu i na podstawie tego zrób sobie warunek.
zegarek84
Document Object Model
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.