First off, I'm brand new to PHP so I'm sorry if this is a stupid question, second of all sorry if this title is incorrect.
Now, what I'm trying to do is create an overlay for a game that I play. My code for the overlay works perfectly, and now I'm working on my HTML file which gets its information from a website and outputs it. The code on the website looks like this:
<span id="example1">Information I want</span>
<span id="example2">More Info I want</span>
...
<span id="example3">And some more</span>
Now what I want to do is create a PHP script which goes in and finds elements by their names and gives me the information in those span tags. Here's what I've tried so far, it's not working however (no surprise):
//Some HTML here
<?php
$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->Load('www.website.com');
echo "Example1: " . $doc->getElementById('example1') . "\n";
?>
//More HTML
To be honest, I have no clue what I'm doing. If anyone could show me an example of how to do this properly, or to point me in the right direction I would appreciate it.
The text between open and close tags is a Text Node.
Just write $doc->getElementById('example1')->nodeValue
Your code seems along the right lines, but you're missing a few things.
First of all, your load call is literally looking for a file named "www.website.com". If it's a remote file, you must include the http:// prefix.
Then, you are attempting to echo out the node itself, whereas you want its value (ie. its contents).
Try $doc->getElementById("example1")->nodeValue instead.
That should do it. You may want to add libxml_use_internal_errors(true); so that any errors in the source file won't destroy your page with PHP errors. Also, I would suggest using loadHTMLFile instead of load, as this will be more lenient towards malformed documents.
you can use getElementById:
$a = $doc->getElementById("example1");
var_dump($a); so you will see what you want to echo or put, or something.
You can also make all the names i HTML as example[] end then foreach the example array, so you can get element by id from example array with just one row of code
Related
I'm using PHP and simple HTML DOM Parser to try and grab song lyrics from a website. The song lyrics are held in a div with the class "lyrics". Here's the code I'm using to try and grab the div and display it. Currently it only returns "Array" onto my webpage. When I jsonify the array I can see that the array is empty.
<?php
include('simple_html_dom.php');
$data = file_get_contents("https://example.com/songlyrics");
$html = str_get_html($data);
$lyr = $html->find('div.lyrics');
echo $lyr;
?>
I know that the Simple HTML Dom Parser is being included correctly, and I have no problem displaying the full webpage when I echo $html with some small changes to the code, however I can't seem to echo just this div. Is there something wrong with my code? Why is $lyr returning an array?
There's nothing wrong with your code.
Why is $lyr returning an array?
It's because a class is considered to be used multiple times. If you var_dump($lyr) instead, you should see all the div-elements found with that class name.
You can either echo $lyr[0] or you can $html->find('div.lyrics',0) to select a specific div element.
I'm attempting to create a page where I input a url and the PHP code uses that to pull page elements from another website to be displayed on my blog post. I haven't even made it as far as the form, right now I just need to understand how to get this code to work so that it displays the page elements within the div with the class "products-grid first odd".
<?php
$homepage = file_get_contents('website');
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$dochtml->getElementsByClassName('products-grid first odd');
echo ????
?>
The PHP DOMDocument object does not appear to have the method getElementsByClassName().
Instead, I think you would have to getElementsByTagName() and then loop through those DOMElements and getAttribute('class') on each and check until you find the right one.
So my school has this very annoying way to view my rooster.
you have to bypass 5 links to get to my rooster.
this is the link for my class (it updates weekly without changing the link)
https://webuntis.a12.nl/WebUntis/?school=roc%20a12#Timetable?type=1&departmentId=0&id=2147
i want to display the content from that page on my website but with my
own stylesheet.
i don't mean this:
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
or an iframe....
I think this can be better done using jquery and ajax. You can get jquery to load the target page, use selectors to strip out what you need, then attach it to your document tree. You should then be able to style it anyway you like.
I would recommend you to use the cURL library: http://www.php.net/manual/en/curl.examples.php
But you have to extract part of the page you want to display, because you will get the whole HTML document.
You'd probably read the whole page into a string variable (using file_get_contents like you mentioned for example) and parse the content, here you have some possibilities:
Regular expressions
Walking the DOM tree (eg. using PHPs DOMDocument classes)
After that, you'd most likely replace all the style="..." or class="..." information with your own.
I'm trying to retrieve the game mode of a server.
This is the code:
<p>
<strong>Grand Bazaar</strong>
<span class="bullet">•</span>
Rush •
<img src="src.png">
</p>
I'm trying to find Rush. I tried this script:
foreach($html->find('p .bullet') as $e)
{
$mode = $e->nextSibling ();
}
But the script just skips "Rush" and continues over to the next tag.
I'm sure you guys know what you're doing better than me.
Could anyone help me out here?
You need to make your questions clearer mate... "I'm trying to retrieve the game mode of a server" <- This is irrelevant in relation with your problem for example.
The problem you're having is that "Rush" is nothing but text, it's not a sibling of .bullet as that would imply Rush being the content of a tag that's a sibling to .bullet, say
<span class="bullet">•</span>
<span>Rush •</span>
<img src="src.png">
If the structure you presented is identical all the time though, and you're using Simple HTML DOM by the looks of the code (http://simplehtmldom.sourceforge.net/), then you could maybe clear the contents of the tag first:
$strong = $html->find('strong'); // I think you can use prevSibling in your example
$strong->innerText = null;
And then just strip_tags() on the whole paragraph and get the text?
I've started using the PHP HTML Dom Parser and still learning it. I've got a problem though, I'm trying to obtain data from a video website, within a certain div tag. I've managed to fix it, but a small problem still remains; it catches all the data from the start of that specific div tag. I would probably need to add a small line of code, something like "[/div]" or something, but I'm completely out of any further ideas.
Here's my code that you can take a look at;
foreach($html->find('div[class=video]') as $key => $info)
{
echo $info->innertext;
}
So, how can I fix this? So it only gets the content inside that div, and not the rest of the file?
Thanks!