ferret147 Posted March 8, 2009 Share Posted March 8, 2009 I am trying to get the contents of a div tag from a web page but I have come across a small problem that I can not figure out. If I use the file_get_contents for a HTML page it works, see code below <?php $page = file_get_contents("http://www.domain.com/index.html"); preg_match_all("/<div id=\"divtag1\">.+<\/div>/", $page, $matches, PREG_SET_ORDER); foreach ($matches as $val) { echo $val[0]; } ?> But if I use it to get the contents of a PHP page it does not! see code below. <?php $page = file_get_contents("http://www.domain.com/index.php"); preg_match_all("/<div id=\"divtag1\">.+<\/div>/", $page, $matches, PREG_SET_ORDER); foreach ($matches as $val) { echo $val[0]; } ?> So my question is, is there another way of doing this like with another PHP command or am I just missing something really silly from my present code? Quote Link to comment https://forums.phpfreaks.com/topic/148483-file_get_contents-help/ Share on other sites More sharing options...
Silverado_NL Posted March 8, 2009 Share Posted March 8, 2009 im not sure if it solves ur problems , but u should try getting the php file with file system path. either c:\inetpub for windows or /var/www for linux. think file_get_contents opens the php file as a website instead of just a file cause ur using http://www.domain.com/index.php default webservers would run the index.php instead of returning its content to file_get_contents. Quote Link to comment https://forums.phpfreaks.com/topic/148483-file_get_contents-help/#findComment-779688 Share on other sites More sharing options...
ferret147 Posted March 8, 2009 Author Share Posted March 8, 2009 Thanks for your reply, but no I still do not get the data!! Quote Link to comment https://forums.phpfreaks.com/topic/148483-file_get_contents-help/#findComment-779703 Share on other sites More sharing options...
.josh Posted March 8, 2009 Share Posted March 8, 2009 php executes its code server side and sends the results to the browser or whatever else is requesting it. It doesn't matter what the extension is. file_get_contents grabs the rendered output, exactly as if you were to rightclick > view source through your browser. Think the first thing I would check is make sure index.php has the same stuff you're trying to match. Quote Link to comment https://forums.phpfreaks.com/topic/148483-file_get_contents-help/#findComment-779719 Share on other sites More sharing options...
redarrow Posted March 8, 2009 Share Posted March 8, 2009 works perfect. <?php $page = file_get_contents("http://simpleforum.ath.cx/friends.php"); preg_match_all("/[a-z]{7}/", $page, $matches, PREG_SET_ORDER); foreach ($matches as $val) { echo $val[0]; } ?> result. friends Quote Link to comment https://forums.phpfreaks.com/topic/148483-file_get_contents-help/#findComment-779733 Share on other sites More sharing options...
ferret147 Posted March 8, 2009 Author Share Posted March 8, 2009 php executes its code server side and sends the results to the browser or whatever else is requesting it. It doesn't matter what the extension is. file_get_contents grabs the rendered output, exactly as if you were to rightclick > view source through your browser. Think the first thing I would check is make sure index.php has the same stuff you're trying to match. I made a .html file and put some random text on the page between a div tag and called it test, I then done exactly the same with a .php page but got the content form a database. It worked on the .html page but not on the .php!!!! Quote Link to comment https://forums.phpfreaks.com/topic/148483-file_get_contents-help/#findComment-779944 Share on other sites More sharing options...
.josh Posted March 8, 2009 Share Posted March 8, 2009 okay, so you understand, take that test.html you made and rename it to test.php. You will see that the it works. But having a php file that is pulling it from the database probably displays it differently, maybe with some \n's or who knows what. depends on your code. I'm pretty sure that your pattern is breaking because your php is generating content that's not the same as your test.html. At the very least, try using an s modifier like so: preg_match_all("/<div id=\"divtag1\">.+<\/div>/s", $page, $matches, PREG_SET_ORDER); But also, you're saying that you made a "test" html file well patterns work differently within different contexts. For instance, that .+ will work for you if you only have 1 closing div tag in your test but you're now talking about your php file outputting content, so if it's outputting a bunch of other closing divs on the whole page, you regex is not going to act as intended, because .+ is greedy and will match up to the very last div. At the very least, you should add a ? after the + to make it non-greedy. But anyways, I can't really do anything more than guess seeing as how you aren't showing the content for which you're trying to regex from. Quote Link to comment https://forums.phpfreaks.com/topic/148483-file_get_contents-help/#findComment-779955 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.