Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/148483-file_get_contents-help/
Share on other sites

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.

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.

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!!!!

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. 

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.