user1159454
user1159454

Reputation: 3317

Data scraping the page JUST written with PHP

I have a page that's written with PHP, and after the PHP writes it, I want be able to search through the HTML source code to find certain tags. Is this impossible/unwise?

I tried using file_get_contents at the end of the script when everything has technically already been written to the HTML, and I think I might have broken my page temporarily that way (Hit a resource limit on my host)

My main goal is trying to figure out how I can use Javascript to alter elements of my page one by one. Which I figure I could do if I could find the html tags I'm trying to change...which the PHP wrote...in the same page.

Very new to Javascript you see.

Upvotes: 0

Views: 114

Answers (4)

Marc B
Marc B

Reputation: 360802

Unless you're capturing the output as you generate the page, e.g.

<?php

ob_start();
.... page building here ...
$page = ob_get_clean();
echo $page;
?>

there will be NOTHING for you to work on. However, if you are capturing as above, then you can simply feed $page into DOM and manipulate it there.

But this begs the question... if you need to change the page after it's been built, why not just change how it's built in the first place?

Upvotes: 0

Okan Kocyigit
Okan Kocyigit

Reputation: 13441

if I did not understand wrong, after page is loaded you want to change your own html output so,

<?php
 echo "<div id='mydiv'></div>";
?>
<script type="text/javascript">

      window.onload = function() {  
        document.getElementById("mydiv").innerHTML = "updated html";  
      }  

</script>

Upvotes: 0

Josh
Josh

Reputation: 12566

You can just right click on your rendered web page in most browsers and choose some variant of 'View Source'. Or, you can cURL your page's content, and view it as a text file.

Also, file_get_contents(); makes a new request to get a page / file's contents. So, if you load a page, and at the bottom, it tries to get the page content, it's going to load a new page, then another, forever. You're creating an infinite loop, and exhausting your allocated resources, as dictated by your hosting provider.

Upvotes: 0

rainecc
rainecc

Reputation: 1502

You could do this fairly easily, client side, with jquery.

If you absolutely need to process it server side with php and you absolutely can't do it while generating the code, you could use ob_start() to capture the output and then ob_get_contents() to drop it into a string before doing ob_end_clean() to flush it to the browser.

Upvotes: 1

Related Questions