Use Php To Get Tags Within A Set Tag Id, Class Or Other From A Website
I'm stuck. I'm trying to extract all html tags, their attributes and their text content that are inside a predefined tag from a remote website. Example:
Solution 1:
You can use getElementById
, then access the nodeValue
:
$doc->loadHTML('<html><body><div id="predefined">... return all this ...</div></body></html>');
$i = $doc->getElementById('predefined');
echo$i->nodeValue;
Solution 2:
Just if you want to learn about JavaScript and jQuery :
Assuming this HTML :
<div id="predefined">... return all this ...</div>
Use this JavaScript code (with jQuery) (I'm trying to make it simple but you'll do better if you learn well) :
// To get the content from a single HTML tagvar theID = "predefined";
var theContent = $("#" + theID).text(); // Or .html() if you want the whole content// To POST the extracted content to your PHP page in order to write to your mySQL db
$.ajax({
type: "POST",
url: "http://www.myapp.com/mypage.php",
data: { id: theID, content: theContent },
success: function(response){ // just in case you need a response from your PHP stuffalert(response);
}
});
Then in your PHP "http://www.myapp.com/mypage.php" :
<?php$id = $_POST["id"];
$content = $_POST["content"];
// Note: Always sanitize posted data, to prevent injections and other malicious code.// Here you can save the data to your MySQL dbecho"it worked!"; // This is the response that your "success" function will get in your Javascript code?>
Well, I'm not sure this is the best answer for your special case, but anyway you really should learn JavaScript =)
Post a Comment for "Use Php To Get Tags Within A Set Tag Id, Class Or Other From A Website"