0

I'm currently using this code:

$blog= file_get_contents("http://powback.tumblr.com/post/" . $post);
echo $blog;

And it works. But tumblr has added a script that activates each time you enter a password-field. So my question is:

Can i remove certain parts with file_get_contents? Or just remove everything above the <html> tag? could i possibly kill a whole div so it wont load at all? And if so; how?

edit:

I managed to do it the simple way. By skipping 766 characters. The script now work as intended!

$blog= file_get_contents("powback.tumblr.com/post/"; . $post, NULL, NULL, 766);
Mats Bakken
  • 155
  • 1
  • 14
  • file_get_contents() won't do it alone. You might have some luck using DOMDocument and loadHTML(), if it's well formatted. Otherwise, preg_replace() could help you. More details would be required to provide an answer to this question. – Louis-Philippe Huberdeau Feb 22 '12 at 22:35

3 Answers3

2

After file_get_contents returns, you have in your hands a string. You can do anything you want to it, including cutting out parts of it.

There are two ways to actually do the cutting:

  1. Using string functions like str_replace, preg_replace and others; the exact recipe depends on what you need to do. This approach is kind of frowned upon because you are working at the wrong level of abstraction, but in some cases it has an unmatched performance to time spent ratio.
  2. Parsing the HTML into a DOM tree, modifying it appropriately (this time working at the appropriate level of abstraction) and then turn it back into a string and echo it. This can be more convenient to work with if your requirements are not dead simple and is easier to maintain, but it typically requires more code to be written.

If you want to do something that's most naturally expressed in HTML document terms ("cutting out this <div>") then don't be tempted and go with the second approach.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thank you for your answer, i'll keep this in mind on future issues! However, I managed to do it the simple way. By skipping 766 characters. The script now work as intended! $blog= file_get_contents("http://powback.tumblr.com/post/" . $post, NULL, NULL, 766); – Mats Bakken Feb 23 '12 at 19:07
1

At that point, $blog is just a string, so you can use normal PHP functions to alter it. Look into these 2:

http://php.net/manual/en/function.str-replace.php

http://us2.php.net/manual/en/function.preg-replace.php

Chris Cherry
  • 28,118
  • 6
  • 68
  • 71
0

You can parse your output using simple html dom parser and display olythe contents thatyou really want to display

dee
  • 194
  • 7