Solved

PHP - extracting values from HTML

Posted on 2014-02-09
4
367 Views
Last Modified: 2014-02-13
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
0
Comment
Question by:mhdi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39846437
... 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.
0
 

Author Comment

by:mhdi
ID: 39846449
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.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39846456
As with most programming questions, the quality of the answer to this question is highly dependent on the quality of the test data set.  This is a rather unusual question because PHP is normally used to create HTML, not to parse HTML.  Nevertheless, it can be done.  It's just a matter of coming up with a representative test data set so that the parsing process takes into account sufficient detail about the inputs and outputs.

Please see http://www.laprbass.com/RAY_temp_mhdi.php

<?php // RAY_temp_mhdi.php
error_reporting(E_ALL);


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28360576.html


// TEST DATA FROM THE POST AT EE
$htm = <<<EOD
<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>
EOD;

/**
 * DESIRED VARIABLES FROM THE TEST DATA
 *
$name = "Link Text"
$link = "link.html"
$phone = "555 123 123"
$address = "1407 Mayfair St, City West"
 */

// MAKE AN OBJECT FROM THE STRING OF HTML STATEMENTS
$xml = '<wrap>' . $htm . '</wrap>';
$obj = simpleXML_Load_String($xml);

// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);

// ASSIGN THE OBJECT PROPERTIES TO LOCAL VARIABLES
$name    = (string)$obj->a;
$link    = (string)$obj->a['href'];
$phone   = trim($obj->span);
$address = $obj->p;

// SHOW THE WORK PRODUCT
echo PHP_EOL
. "NAME: $name, "
. "LINK: $link, "
. "PHONE: $phone, "
. "ADDRESS: $address"
;

Open in new window

Best regards, ~Ray
0
 
LVL 15

Assisted Solution

by:Insoftservice
Insoftservice earned 250 total points
ID: 39846473
You could try below link  it might guide you to resolve your issue.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27981504.html
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How Close unsubmited attempts 10 46
How to use 2 ON statements in inner join 3 27
Regular Expression needed 4 17
How to remove  "0":  in a json string 4 17
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question