PHP Micro library to extract links from an HTML string.
The easiest way to install the library is using Composer.
{
"require": {
"piradoiv/linklord": "1.*"
}
}
Please notice the library requires at least PHP 5.3 in order to work (tested on 5.3.26 and 5.4.16)
# Require Composer autoloader
require 'vendor/autoload.php';
$html = '<html><body><a href="http://www.example.com/">Example</a></body></html>';
$parser = new \PiradoIV\Html\LinkLord\Parser($html);
$links = $parser->getLinks();
foreach ($links as $node) {
echo "{$node->anchorText}\n";
}
Follow/Nofollow
LinkLord is able to know whether the links are followed or not.
$node->isNoFollow;
What about images?
If the link has an image child, it will be detected.
$node->isImage;
Mentions
Looking for mentions on the code?, LinkLord is able to recognise mentions on the text, this means if there is a 'www.example.com' on the text and is not linked, this will count as a mention.
$possibleMentions = array('www.example.com', 'www.anotherdomain.com');
$mentions = $parser->getMentions($possibleMentions);
If you need more examples or help, please contact me on Twitter, @PiradoIV :)
Enjoy! :)