0

I've been trying for a while now, but never really understood Regex. How can I split this string so I get the age of the domain? Thanks.

<a target=_blank title='View how the website looked at this Age' href=website-history.php?archiveCreationTime=2013050316413&domain=domain.net>0 years 9 months old</a>

btw, the code is part of a html source.

Sorry, might be a noob question. But I've never got the time to learn Regex. And I tried with explode, but I coulden't make it get the age.

Anders
  • 513
  • 2
  • 10
  • 32
  • 2
    Using a DOM Parser better suit this task. I believe a good PHP one is [SimpleHTMLDOM](http://simplehtmldom.sourceforge.net/) – Justin Wood Feb 20 '14 at 01:03

2 Answers2

3

You shouldn't use regular expressions for parsing HTML. You should use tools designed for this like DomDocument. Here's a basic example:

<?php
$string = "<a target=_blank title='View how the website looked at this Age' href=website-history.php?archiveCreationTime=2013050316413&domain=domain.net>0 years 9 months old</a>";
$dom = new DOMDocument();
@$dom->loadHTML($string);
$anchor = $dom->getElementsByTagName('a')->item(0);
echo $anchor->nodeValue;

See it in action

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

You can use phpQuery to do this. If you need a practical example, you can read how to scrape anchor tags. There's some relevant code there to show you how to get at the anchor tags and pull out the inner text for the anchor node.

Kerry Kobashi
  • 806
  • 6
  • 9