Results 1 to 4 of 4

Thread: Grab content in PHP

  1. #1
    Join Date
    May 2008
    Posts
    222

    Grab content in PHP

    Hi
    I want to grab content and put them in the PHP array, but I have a problem. Please help. I am posting my code below.
    Code:
    $html = '<div class="pd">
        <div class="section">
            <div class="section_title">Title text</div>
        </div>
        <div>
            <div><small>Info.</small></div>
        </div>
        <div class="section">
            <div><a href="#">link</a> grab content <small>12/2/2009</small></div>
            <div><a href="#">link</a> grab content <small>12/2/2009</small></div>
            <div><a href="#">link</a> grab content <small>12/2/2009</small></div>
            <div><a href="#">link</a> grab content <small>12/2/2009</small></div>
            <div><a href="#">link</a> grab content <small>12/2/2009</small></div>
        </div>
    </div>';
    Any help on this.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Grab content in PHP

    Hi
    Do not know how others will solve this problem. But I have my own way, just check the following code and it will help you.
    Code:
    $html = '<div class="pd">
        <div class="section">
            <div class="section_title">Title text</div>
        </div>
        <div>
            <div><small>Info.</small></div>
        </div>
        <div class="section">
            <div><a href="#">link</a> grab content <small>12/2/2009</small></div>
            <div><a href="#">link</a> grab content <small>12/2/2009</small></div>
            <div><a href="#">link</a> grab content <small>12/2/2009</small></div>
            <div><a href="#">link</a> grab content <small>12/2/2009</small></div>
            <div><a href="#">link</a> grab content <small>12/2/2009</small></div>
        </div>
    </div>';
    // Skip first <div class="section">
    $div1 = stripos($html,"<div class=\"section\">");
    $html = str_ireplace(" ","",substr($html, stripos($html,"<div class=\"section\">",($div1+11))));
    
    $out = arr();
    $intK = 0;
    //Strip away <div class="section"> and explode string into arr 1 element / row
    $prt = explode("\n",substr($html, stripos($html,">")+2));
    foreach ($prt as $val) {
     </div>
        if(trim($val) != "</div>") {
            $out[$intK++] = $val;
        }
    }
    print_r($out);

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Grab content in PHP

    Hi
    The above posted solution will definitaley solve your problem, but here is another option for your to solve it. just see the part of a code and implement which you feel easy.
    Code:
    if(preg_match_all('~<div><a[^>]*?>[\s\S\w]*?</a>[\s]*([\s\S\w]+?)<small>([\d\s/]+)</small></div>~i',$html,$mtch)){
      echo '<pre>';
      print_r($mtch[1]);
      print_r($mtch[2]);
      echo '</pre>';
    }
    The output of the code in your case is something like this.
    Code:
    arr
    (
        [0] => grab content
        [1] => grab content
        [2] => grab content
        [3] => grab content
        [4] => grab content
    )
    arr
    (
        [0] => 12/2/2009
        [1] => 12/2/2009
        [2] => 12/2/2009
        [3] => 12/2/2009
        [4] => 12/2/2009
    )

  4. #4
    Join Date
    May 2008
    Posts
    222

    Re: Grab content in PHP

    Hi
    Thanks for both.
    The code worked properly and it is good to see that you guys have quickly replied to my query. Thanks a lot again.

Similar Threads

  1. Mac software to grab irregular region
    By Oorja in forum Windows Software
    Replies: 5
    Last Post: 26-02-2012, 10:13 AM
  2. How to remove screen.grab.j.exe
    By Hodge Broderick in forum Networking & Security
    Replies: 3
    Last Post: 31-01-2011, 07:51 PM
  3. GRAB PC's first Computer Showroom in Chennai
    By sportsfan81 in forum Off Topic Chat
    Replies: 2
    Last Post: 12-05-2009, 12:17 PM
  4. hp all in one won't grab anymore
    By Jenifercastaneda in forum Hardware Peripherals
    Replies: 4
    Last Post: 10-01-2009, 09:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,391,592.32399 seconds with 16 queries