Link to home
Start Free TrialLog in
Avatar of mhdi
mhdi

asked on

PHP - extracting values from HTML

Hi,

I have the following HTML block in a variable called $innerHTML.
<a class="name" href="link.html">Link Text</a>
<span class="phone">
555 123 123
</span>
<p class="address secondary">1407 Mayfair St, City West</p>

Open in new window


I need to extract the following data out -

$name = "Link Text"
$link = "link.html"
$phone = "555 123 123"
$address = "1407 Mayfair St, City West"

What is the best way?

Ive attempted to use Xpath Queries but it doesnt seem efficient as it requires a foreach loop to go through the query result.

Any suggestions are much appreciated.

Thankyou
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

... it doesnt seem efficient as it requires a foreach loop ...
How have you concluded this is not efficient?  Have you measured the elapsed time?  If you haven't, you might want to do that before you dismiss this out-of-hand.  In my experience you cannot find the inefficiency in any reasonable human measure.  You may be talking microseconds, but you're not anywhere near milliseconds with something like this.

Please post the code you've tried and I'll show you how to measure the elapsed time.
Avatar of mhdi
mhdi

ASKER

The reason I don't like using xpath queries is because it returns a list of matching nodes which I then need to go over with a foreach loop to extract the nodevalue.  As my html block only has one occurance of each item it doesnt seem like a good way to do it.

Maybe I should have said easier rather than inefficient.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial