0

I am trying to link my page to another website in which I can use there div tags in order to keep my site up to date.

I've got some code after some research and it's echoing out just 1 string whereas there are multiple div classes on the page and I would like to echo them all. I am just wondering if this is possible or not?

Here is the current code:

<?php
$url = 'http://www.domain.com';
$content = file_get_contents($url);
$activity = explode( '<div class="class">' , $content );
$activity_second = explode("</div>" , $activity );

echo $activity_second[0];
?>

I can echo $activity_second[0] which will display the first line and $activity_second[1] which will display the second line.

However, I am looking to expand this over to allow all of the div classes on the same page to be put into an array which can then be echo'd out into different parts of a table.

Thank you for your help in advance.

fusion3k
  • 11,568
  • 4
  • 25
  • 47
miotk
  • 37
  • 1
  • 10
  • Please can you edit question adding example of original html and desired result? – fusion3k Mar 16 '16 at 15:43
  • I don't have the original html as it's external, however the div class is `
    ` which there is multiple divs with the same class. I am hoping to extract this information from the website and can echo the results onto my own site.
    – miotk Mar 16 '16 at 15:49

4 Answers4

1

Let me see if I get it straight, you have something like this:

<div id="another-class"><div class="class">some text 1</div></div>
<div class="class">some text 2</div>
<div class="class">some text 3</div>
<div class="class">some text 4</div>
<div class="class">some text 5</div>
<div class="class">some text 6</div>

And you need the text contained the div elements. If this is correct, replace:

$activity = explode( '<div class="class">' , $content );
$activity_second = explode("</div>" , $activity );

with this:

preg_match_all('#<div class="class">(.+?)</div>#', $content, $matches);

In this example, after the function call $matches will have the following:

Array
(
    [0] => Array
        (
            [0] => <div class="class">some text 1</div>
            [1] => <div class="class">some text 2</div>
            [2] => <div class="class">some text 3</div>
            [3] => <div class="class">some text 4</div>
            [4] => <div class="class">some text 5</div>
            [5] => <div class="class">some text 6</div>
        )

    [1] => Array
        (
            [0] => some text 1
            [1] => some text 2
            [2] => some text 3
            [3] => some text 4
            [4] => some text 5
            [5] => some text 6
        )

)

The data you need is in $matches[1].

Eduardo Galván
  • 962
  • 7
  • 15
  • I did this and I'm getting the same as before just Array is being displayed on the website – miotk Mar 16 '16 at 17:36
  • You have got the concept correct though, that is what I am hoping to achieve – miotk Mar 16 '16 at 17:40
  • `(.+?)#', $content, $matches); echo $matches[1]; ?>` is this correct, is this how it is meant to look? The code or am I missing something? – miotk Mar 16 '16 at 17:49
  • $matches[1] is an array, so echoing it just like that won't work. For that you need a foreach statement, like this: `foreach($matches[1] as $match) { echo $match; }` – Eduardo Galván Mar 16 '16 at 20:03
0

The problem probably is that only the first key of the first array gets into the second explode. Try the following after $activity:

$result = array();

foreach ($activity as $div){
    $handle = explode("</div>", $div);
    $result -> append($handle);
}
foreach ($result as $key){
   echo $key;
}

I'm sorry for the original reply, I misunderstood your question.

The regex way will work as well.

InterCity
  • 102
  • 7
0

If your intention is to get all contents of divs with that class name, you can use regex capturing those strings between the tags of those divs:

preg_match_all('/<div class="class">([^<]+)<\/div>/', $content, $m);

print_r($m[1]);

Now $m[1] will be an array containing all those inner HTML strings of those divs.

n-dru
  • 9,285
  • 2
  • 29
  • 42
0

The rule is: when I act with HTML, I have to use a parser.

Assuming you have a HTML document like this:

$html = '<html>
<head><title>Untitled</title></head>
<body>
    <div class="class">
        <b>My Content 1</b>
    </div>
    <div class="class">
        <b>My Content 2</b>
    </div>
    <div class="class">
        <b>My Content 3</b>
    </div>
</body>
</html>';

load it into a DOMDocument object, init a DOMXPath object based on loaded HTML:

$dom = new DOMDocument();
libxml_use_internal_errors(1);
$dom->formatOutput = True;
$dom->loadHTML( $html );
$xpath = new DOMXPath( $dom );

and with this command you can access to all <div class="class">:

foreach( $xpath->query( '//div[@class="class"]' ) as $node )
{
    echo trim( $node->nodeValue ) . '<br>';
}

Your output:

My Content 1
My Content 2
My Content 3

If you want echo the node as HTML, replace echo ...

with:

echo $dom->saveHTML( $node );

will output:

<div class="class">
    <b>My Content 1</b>
</div>
<div class="class">
    <b>My Content 2</b>
</div>
<div class="class">
    <b>My Content 3</b>
</div>

At last, if you want echo only the innerHTML of the nodes, you have to write something like this:

foreach( $xpath->query( '//div[@class="class"]' ) as $node )
{
    foreach ($node->childNodes as $child) 
    { 
        echo $dom->saveHTML( $child );
    }
}

and your output will be:

<b>My Content 1</b>
<b>My Content 2</b>
<b>My Content 3</b>

Community
  • 1
  • 1
fusion3k
  • 11,568
  • 4
  • 25
  • 47