2

I can access all html codes in a remote website with this code;

<?php

$homepage = file_get_contents('http://www.example.com/');
echo $homepage;

?>

But i want to get only div contents tagged with photo_s class like following code ;

<div class="photo_s">
    <a href="">
        <img title="" src="" alt="" /><br>Example Text <br>Example Text </a>
</div>

The page have 100+ Item tagged with photo_s class. I want to turn it a loop so i can add these items in my database. i want to learn SIMPLE way to do it

Below the Radar
  • 7,321
  • 11
  • 63
  • 142
Emre Y
  • 51
  • 1
  • 7

2 Answers2

2

you can do this in PHP like below

$url = 'https://www.example.com/';
$content = file_get_contents($url);
$first_step = explode( '<div class="photo_s">' , $content );
$second_step = explode("</div>" , $first_step[1] );
echo $second_step[0];

or by jQuery.. try this

<div id="content"></div>
<script>
var href = "www.example.com";
    $.ajax({
       url:href,
       type:'GET',
       success: function(data){
           $('#content').html($(data).find('.photo_s').html());
       }
    });
</script>
Sarath
  • 2,318
  • 1
  • 12
  • 24
  • Thank you but it will give me one item tagged with photo_s, how can i access all of items tagged with photo_s ? – Emre Y Oct 16 '15 at 11:30
0

UPDATED:

for multiple occurrences you can try this:

In PHP:

    <?php  
    $url = 'testing.php';
    $content = file_get_contents($url);
    function getContents($str, $start, $end) {
      $contents = array();
      $startDelimiterLength = strlen($start);
      $endDelimiterLength = strlen($end);
      $startFrom = $contentStart = $contentEnd = 0;
      while (false !== ($contentStart = strpos($str, $start, $startFrom))) {
        $contentStart += $startDelimiterLength;
        $contentEnd = strpos($str, $end, $contentStart);
        if (false === $contentEnd) {
          break;
        }
        $contents[] = substr($str, $contentStart, $contentEnd - $contentStart);
        $startFrom = $contentEnd + $endDelimiterLength;
      }

      return $contents;
    }

    $data = getContents($content, '<div class="photo_s">', '</div>');

    foreach($data as $value){
    echo $value;
}

output:
Example Text
Example Text
Example Text1
Example Text1

Using Jquery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="content"></div>
    <script>
    var href = 'testing.php';
        $.ajax({
           url:href,
           type:'GET',
           success: function(data){
               var str="";
               $(data).each(function(key,value){
                   if(this.className == "photo_s"){
                    str +=typeof value.innerHTML != 'undefined' ? value.innerHTML :"";
                   }
               });
               $('#content').html(str);
           }
        });
    </script>

output:
Example Text
Example Text
Example Text1
Example Text1

test case testing.php:

<div class="photo_s">
    <a href="">
        <img title="" src="" alt="" /><br>Example Text <br>Example Text </a>
</div>

<div class="photo_s123">
    <a href="">
        <img title="" src="" alt="" /><br>Example Text123 <br>Example Text123 </a>
</div>
<div class="photo_s">
    <a href="">
        <img title="" src="" alt="" /><br>Example Text1 <br>Example Text1 </a>
</div>
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44