Hello.
How can i get the html tags and texts of a web page in php?

Dani AI

Generated

A short, practical plan that follows the thread: fetch the raw page, inspect/save the raw HTML (so you can see what the server actually returned), then parse it with a proper HTML parser. As noted, a DOM-style parser is the right direction; 's Simple HTML DOM is convenient for quick work; and 's trouble with file_get_html() on google.com is exactly the kind of failure you'll hit when the remote site blocks non-browser requests or when PHP code has small errors (see notes below).

Use a robust fetch (cURL) so you see status codes, redirects and can set a browser user‑agent. Save the raw HTML for debugging:

function fetch_page($url) {
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_MAXREDIRS => 5,
        CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
        CURLOPT_ENCODING => '',        // accept gzip/deflate
        CURLOPT_TIMEOUT => 15,
    ]);
    $html = curl_exec($ch);
    $info = curl_getinfo($ch);
    $err  = curl_error($ch);
    curl_close($ch);
    return ['html'=>$html, 'http_code'=>$info['http_code'], 'error'=>$err];
}

Parse and write out the combined tags+text with DOMDocument (use internal libxml errors and convert encoding). The call below produces normalized HTML you can open or save:

libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding($rawHtml, 'HTML-ENTITIES', 'UTF-8'));
$fullHtml = $dom->saveHTML();
file_put_contents('page.html', $fullHtml);

If you need a node's inner HTML (children plus text), use a helper:

function innerHTML(DOMNode $n) {
    $s = '';
    foreach ($n->childNodes as $c) { $s .= $n->ownerDocument->saveHTML($c); }
    return $s;
}

Troubleshooting notes: replace any “smart quotes” with straight ASCII quotes if code is failing; check PHP settings (allow_url_fopen, open_basedir) if file-based fetches fail; many modern sites (Google, Cloudflare-protected pages, SPAs) return different content to non-browsers or require JavaScript — for those you need a headless browser (headless Chrome/Puppeteer or a render service). Respect robots.txt and site terms; log HTTP status and curl_error() if you get empty output.

Recommended Answers

All 8 Replies

Well, like in python.... for example with a web crawler (with BeautifulSop if I'm correct) that we can get all html tags and text of a web page and then save it to a text file.

I want to do the same with php, give the url of a web page and receive it's html tags and contents.

Member Avatar for Member #120589

file_get_contents() will take the entire file. Alternatively use cURL?

Would you give me an example please?
An example that you give a url to the script, and it gets all the html tags and text of that page and echo it on a file. I want the output to have text and html tags together.

I found an example myself:

    $url = “

    $str = file_get_contents($url);

Thank you @diafol.

my best choice is
Click Here
linear and well document also works well.

, file_get_html() didn't work for me because of the html word in it and had to replace it with the word contents to get a result.

and also with:

foreach($html->find('img') as $element)
       echo $element->src . '<br>';

foreach($html->find('a') as $element)
       echo $element->href . '<br>';

there was no result and nothing appeaed on the page. (for www.google.com).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.