file get contents & preg match all - problem

Discussion in 'PHP' started by the croner, Dec 23, 2008.

  1. #1
    Hi , I want to get content located between <div class="tresc"> </div> , from website and show it on mine ,Im using file get content to obtain web source , then preg match all to get content between divs , but its not working , return nothing

    the target div

    
    <div class="tresc">Od
    poniedziałku
    do
    piÄ…tku
    o&nbsp;godz.
    <strong>19:50</strong><br>Weekendy
    o&nbsp;godz.
    <strong>19:45</strong></div>
    PHP:
    And my code , propably the pattern is wrong , so plz help me
    <?php
    
    
    
    function file_get_contents_utf8($fn){
        $fn ="http://uwaga.onet.pl/index.html";
         $content = file_get_contents($fn);
          return mb_convert_encoding($content, 'UTF-8',
              mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
    }
    
    
    function findinside($tag, $content)
    {
        $pattern = '/<div class='.preg_quote($tag).' >(.*?)<div>/si';
        preg_match_all($pattern, $content, $m);
        return $m[1];
    }
    
    
    $tag = 'tresc';
    $data="<div class=\"tresc\">text1<strong>text2</strong><br><strong>text3</strong></div>";
    $out = findinside($tag, $data);
     print_r ($out);
    PHP:
     
    the croner, Dec 23, 2008 IP
  2. diligenthost

    diligenthost Peon

    Messages:
    685
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Before I look through the code, have you made sure you can use file_get_contents with URLs? Sometimes hosts disallow this function as a security feature. Check that allow_url_fopen is enabled in a phpinfo file.
     
    diligenthost, Dec 23, 2008 IP
  3. the croner

    the croner Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I enabled it and now it returns : Array ( ) , so what now ?
     
    the croner, Dec 23, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    <?php
    
    function file_get_contents_utf8( $fn )
    { /* We'll run with your function */
        $content = file_get_contents( $fn );
        return mb_convert_encoding( $content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true) );
    }
    
    $file = file_get_contents_utf8('http://uwaga.onet.pl/index.html');
    preg_match('/<div class=tresc>(.*?)<\/div>/', str_replace(array("\n", "\r"), ' ', $file), $matches);
    list(, $value) = $matches;
    
    echo $value;
    
    ?>
    PHP:
    Returns:
    Od poniedziaÂłku do piÂątku o&nbsp;godz. <strong>19:50</strong><br>Weekendy o&nbsp;godz. <strong>19:45</strong>
    Code (markup):
     
    Danltn, Dec 23, 2008 IP
  5. the croner

    the croner Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Oh Thanks man very much , works fine.
     
    the croner, Dec 23, 2008 IP