Get a specific content block of element using URL in PHP

file-get-contents, php

Solution

Another way would be to use regex.

<?php

$string = '<html> <body> 
    <div id="myContent"> 
        <ul>     
            <li></li> 
            <li></li> 
            <li></li> 
        </ul> 
    </div>  
</body> </html>';

if ( preg_match ( '/<div id="myContent"(.*?)<\/div>/s', $string, $matches ) )
{
    foreach ( $matches as $key => $match )
    {
        echo $key . ' => ' . htmlentities ( $match ) . '<br /><br />';
    }
}
else
{
    echo 'No match';
}

?>

Live example: http://codepad.viper-7.com/WSoWCh

Problem

Possible Duplicate: How to parse and process HTML with PHP? I know file_get_contents(url) method, but i wanted is that maybe using file_get_contents(url) at first to pull the contents of a page then is there something methods/functions that can extract or get a certain block of contents from the contents that you get using file_get_contents(url)? Here's a sample: so the code will be like this: ``` $pageContent = file_get_contents('http://www.pullcontentshere.com/'); ``` and this will be the output of `$pageContent` ``` <html> <body> <div id="myContent"> <ul> <li></li> <li></li> <li></li> </ul> </div> </body> </html> ``` Maybe you have something to suggest or have in mind how to specifically extract the `<div id="myContent">` and the entire children of it? So it will be something like this: ``` $content = function_here($pageContent); ``` so the output would be like this: ``` <div id="myContent"> <ul> <li></li> <li></li> <li></li> </ul> </div> ``` Answers are greatly appreciated!

Original source

Related problems