Jump to content

HTML Parsing help :)


Mike088

Recommended Posts

Hey everyone,

 

I'm a little stuck here, I've just started playing around with DOM and parsing. I'm down to the very last thing that I want to grab and then display which is just standard text on my html page, it doesn't have any attributes or title tags; it's just plain text... and I can't seem to grab it  :'(

 

Here's the html code I'm trying to parse (in bold is what I'm after) giving it the value $price:

 

<body>

$10.00<a href="http://www.site1.com/page1/data/help" title="Site1">Site1</a><br/>

$20.00<a href="http://www.site2.com/page1/data/help" title="Site2">Site2</a><br/>

$30.00<a href="http://www.site3.com/page1/data/help" title="Site3">Site3</a><br/>

$40.00<a href="http://www.site4.com/page1/data/help" title="Site4">Site4</a><br/>

</body>

 

Here's where I'm at:

 

// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab text from the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
$url2 = $href->getAttribute('title');
        $price = "not sure what to put here...";
$newurl = str_replace("page1","page2",$url);
$newurl2 = str_replace("help","helpme",$newurl);
echo "<br /> From". $price ." <a href='" . $newurl2 . "'>$url2</a>";
}

 

Any help would be much appreciated,

Cheers  ;)

Link to comment
Share on other sites

Ugh well do you know of any other ways to grab that html and display it? I guess I'm just basically duplicating the content but replacing text within the links. The html is dynamically generated and I need to swap out text within the links so that's why I can't just use it or an iframe  :(

Link to comment
Share on other sites

Thanks mrMarcus that 'file_get_contents' does the trick well and overall makes everything alot easier too :thumb-up:

 

This is what I ended up with:

 

$html = file_get_contents("http://www.website.com/page");

$html = str_replace("page1","page2",$html);
$html = str_replace("help","helpme",$html);

echo $html;

 

To easy.

 

Cheers,

Mike.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.