user580523
user580523

Reputation:

PHP Regular expressions problem

I have this simple phrase that I need the last url value of.

<h4>Location</h4><a href="link">NOT NEEEDED</a> > <a href="link">NOT NEEDED</a> > <a href="link">NEED</a><br />

The HTML is constant is regards to the layout, the only thing that will change is the number of links.

I was wondering if anybody has any thoughts on always grabbing the last link's value, even if there is only one link.

Upvotes: 0

Views: 74

Answers (3)

Matthew Turland
Matthew Turland

Reputation: 763

I concur with alex; you generally don't want to use regular expressions to parse markup. If you have any issues with malformed markup while using his example, try using the tidy extension to clean it up first: http://php.net/manual/en/tidy.examples.basic.php

Upvotes: 0

alex
alex

Reputation: 490243

It has been regurgitated hundreds of times, the ol' dangers of using regex to parse HTML.

Whilst I'm sure a regex would suffice for this subset of HTML, I still believe you're better off using...

$dom = new DOMDocument;

$dom->loadHTML($html);

$anchors = $dom->getElementsByTagName('a');

$lastHref = $anchors->item($anchors->length - 1)->getAttribute('href');

CodePad.

Much more robust IMO.

Upvotes: 3

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

You could try this:

preg_match_all("/href=\"([^\"]+)\"/",$html,$m);
$last = array_pop($m[1]);

Upvotes: 1

Related Questions