1

I wanna extract some html from page Y

for example site is
<head>xxxx</head>
<body>....<div id="ineedthis_code"> </div> ...</body>

it is possible to do this file_get_contents ?! i need only that div nothing else

VicToR Sava
  • 101
  • 1
  • 4
  • 10
  • 1
    Have a look at this and use one of these libraries http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-xml-with-php – Alexey Mar 26 '13 at 17:33
  • The above comment is a good answer to what you need. You say you need only a particular
    , but you're going to need to collect the entire page anyways. It's then you can use DOM or phpQuery to specifically target your extracted content.
    – MackieeE Mar 26 '13 at 17:40

1 Answers1

2

Without using a special library (which is the best way in most cases), you can use the explode-function:

$content = file_get_contents($url);
$first_step = explode( '<div id="YOUR ID HERE">' , $content ); // So you will get two array elements

$second_step = explode("</div>" , $first_step[1] ); // "1" depends, if you have more elements with this id (theoretical) 

echo $second_step[0]; // You will get the first element with the content within the DIV :)

Please note, it's only an example without error handling. It also works onlny on a special case; not if the html structure ist changing. Even simple spaces can break this code. So you should better use a parsing library ;-)

James111
  • 15,378
  • 15
  • 78
  • 121
Stefan Brendle
  • 1,545
  • 6
  • 20
  • 39