PHP get divs from HTML code

Community Forums/General Help/PHP get divs from HTML code

Arska(Posted 2012) [#1]
Hello!

I got job to create PHP code that gets content of 'div' from HTML code.

So first of all i don't have any ideas how to get HTML code from page with PHP code.

Then i need to pickup content of div.

Example HTML code:
<div id="widget-ad-contact" class="widget box"> 
<h3>Person</h3><div class="clear"></div>
 <strong>Name:</strong>Someone<br><strong>Phone:</strong> 050-12345678<br>
</div>
</div>


I want PHP code to input something like this:
Name: Someone
Phone: 050-12345678

Thanks for your help.


xlsior(Posted 2012) [#2]
http://bit.ly/GOCuhU


D4NM4N(Posted 2012) [#3]
Im not really sure what you are asking. Do you want it to 1) inject values into the HTML like name and number or 2) actually parse some HTML structures and make a kind of "hashmap" of the DIV's properties?

If number 1 then simply put your $values in the echo. If number 2 why are you needing that? surely your PHP code is generating the HTML, or do you have something else in mind?


xlsior(Posted 2012) [#4]
To me the biggest question is why someone would take a job implementing something like this when they apparently have no idea how to even begin to approach it. :-?


Arska(Posted 2012) [#5]
It was a training job, but nevermind i don't need help anymore.


Derron(Posted 2012) [#6]
Although he doesn't need it anymore, maybe someone else wants a working solution (i don't like unanswered questions)

$url = "http://www.toyourdomain.com/butfast/index.html";
//check cache
//...else get content and refresh/save/bla
$result	= @file_get_contents($url);

$regex = "/<div>(.*?)<\/div>/s"; 				//step 1... get all "div"
preg_match_all($regex, $result, $divs);
foreach($divs[1] as $div) {					//step 2... get all "details"
	//work with the content of the div - another regex or ...
	//in your case:
	preg_match("/<strong>Name:</strong>(.*?)<br>/s", $div, $title);
	print "title: ".$title[1];
}
//...continue with rest of app


Of course you could decide to make your regex a less greedy or more dynamic (also accepting <strong><span>name:...)

bye
Ron