Get title from link, PHP Simple HTML DOM Parser - php

I'm trying to get the title from a link - which I already have identified, it is just the last bit with retrieving the title from the same link.
To find the right link I use this code:
$html->find('a[href=http://mylink.se']');
But I also want the title from this link. How would I do that?

Assuming you're using PHP Simple HTML DOM parser, which is not clear in your question, you could do
$link = $html->find('a[href=http://mylink.se]', 0); //As the OP pointed out in comments, you need to select the first element
$title = $link->title
http://simplehtmldom.sourceforge.net/manual.htm

Related

Get specific URL with PHP Simple HTML DOM Parser

So for my code I basically need to get a specific URL from a webpage, I use Simple HTML DOM Parser, so far I managed to get all the links from the webpage with the class .linkify, which results in having 18 different links, and I will need to have only the second one in a variable.
Here's my code:
$html = file_get_html("http://saucenao.com/search.php?db=999&url=http://simg4.gelbooru.com//images/4f/3d/$file");
foreach($html->find('a.linkify') as $element)
echo $element->href . '<br>';
How do I make it so it only generates the second link?
Thanks!
You can select which one you want by modifying
$html->find('a.linkify')
to
$html->find('a.linkify', 1)
To fetch the 2nd one, we use ,1. It's index based so we start at 0 (0 = First one, 1 = Second one)
To get the href in a single line, do the following:
$html->find('a.linkify', 1)->href

HTML doesn't get rendered, why does it happen?

On a PHP+MySQL project, there's a string of text coming from a MySQL table that contains HTML tags but those tags never get rendered by Google Chrome or any browser I tried yet:
You can see that the HTML (p, strong) aren't getting interpreted by the browser.
So the result is:
EDIT: HTML/PHP
<div class="winery_description">
<?php echo $this->winery['description']; ?>
</div>
$this->winery being the array result of the SQL Select.
EDIT 2: I'm the dumbest man in the world, the source contains entities. So the new question is: How do I force entities to be interpreted?
Real source:
Any suggestions? Thanks!
You are probably using innerText or textContent to set the content of your div, which just replace the child nodes of the div with a single text node.
Use innerHTML instead, to have the browser parse the HTML and create the appropriate DOM nodes.
The answer provided by #Paulpro is correct.
Also note that if you are using jQuery, be sure to use the .html() method instead of .text() method:
$('#your_element').html('<h1>This works!</h1>');
$('#another_element').text('<h2>Wrong; you will see the <h2> in the output');

Getting Specific Data in PHP

What is the way to get specific data using PHP. In this case i want to get some text which is wrapped by <span class="s"> to the first <b> HTML tag.Assuming a HTML source code is:
Once there was a king <span class="s"> May 3 2009 <b> ABC Some Text </b> Some photo or video</span> but they have...
So, here i want to get those filtered data in a variable like: $fdata = "May 3 2009";Because, May 3 2009 is wrapped by <span class="s"> to the first <b> HTML tag.
I will use it in SIMPLE PHP HTML DOM PARSING. So, any idea or example to filter those text and get it in a variable? Any idea will be a great help. *If you found a duplicate question here, its not that its more specified.
Use Simple HTML DOM
http://simplehtmldom.sourceforge.net/
Or http://php.net/manual/en/domdocument.loadhtml.php
Or you can use any other library also.
If you're using simple html dom parser you'd grab the elements you're targeting like this:
$ret = $html->find('span class="s"');
This is just a basic sample, but it should get you going in the right direction.
if you need to find a very specific instance, you can use something such as:
$ret = $html->find("#div1", 0)->children(1)->children(1)->children(2)->id;

Playing Simple PHP Dom Parser In Advance

I want to do something advanced with PHP Simple HTML DOM Parser but can't understand how to do so..
What I want to do: Suppose we are getting some url from here
foreach($result->find('a') as $element)
$element->href
and now i want to get its content as plain text but note that I am going to fetch full content from that pages.
Suppose this script has a variable called $sn = 'linkin park song'; - is there any way to fetch lines from those pages which have the keywords from $sn
If you don't understand what I want to do then here is a example.
Suppose $sn = 'Google 2012 Earning Report'; so PHP SIMPLE DOM PARSER will get some url from $element->href , such as:
http://www.dailytech.com/Google+Q2+2012+Earnings+Rise+11+Percent+Deals+with+Falling+Ad+PricesMotorola+Issues/article25221.htm
It will fetch plain text from this url, which match $sn:
Google's Q2 2012 earning are looking good despite certain issues ...
Google's latest Earning Report is result of its ability to better target...

Get a single element with PHP and XPath

Lots of tutorials around the net but none of them can explain me this:
How do I select a single element (in a table, for example), having its absolute XPath?
Example:
I have this:
/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table[3]/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[4]/td[5]/span
What's that PHP function to get the text of that element?!
Really I could not find an answer. Found lots of guides and hints to get all the elements of the table, all the buttons of a form, etc, but not what I need.
Thank you.
$xml = simplexml_load_string($html_content_string);
$arr = $xml->xpath("//body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table[3]/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[4]/td[5]/span");
var_dump($arr);
Load you HTML document into a DOM object then make a DOMXPath object from it and let it evaluate your query string.
It's all described in detail here: http://php.net/manual/en/book.dom.php

Categories