Jump to content

Problem retrieving HTML tags from DOMXPath


Recommended Posts

I have two codes at the bottom. The first code is a scraper file that captures and displays the contents of a web page from the "div" tag.

index code:

function curlGet($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    $results = curl_exec($ch);
    curl_close($ch);
    return $results;
}

function returnXPathObj($item)
{
    $xmlPageDom = new DomDocument();
    @$xmlPageDom->loadHTML($item);
    $xmlPageXPath = new DOMXPath($xmlPageDom);
    return $xmlPageXPath;
}

echo returnXPathObj(curlGet('http://localhost/test/page'))->query('//div')->item(0)->nodeValue; // This URL refers to a page that contains the following code

The second code is the contents of that web page

webpage code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="content">
        <h1>phpfreaks</h1>
        <p>www.phpfreaks.com</p>
    </div>

    <span class="content2">
        <h1>phpfreaks</h1>
        <p>www.phpfreaks.com</p>
        </spans>
</body>

</html>

Now when I run the code, it only shows the text and hides the HTML tags. 

How can I display the contents of the Dave tag along with all the HTML tags?

for example:

Now displays as follows:

phpfreaks www.phpfreaks.com

I want to show the following:

<h1>phpfreaks</h1>
<p>www.phpfreaks.com</p>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.