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
https://forums.phpfreaks.com/topic/199482-html-parsing-help/
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
https://forums.phpfreaks.com/topic/199482-html-parsing-help/#findComment-1046986
Share on other sites

check out file_get_contents and preg_match / preg_replace .. might be what you're looking for.

 

Side Note: when in development, don't suppress your functions/code with @, as you will not be able to see what is creating an error if there is one.  just handle it with conditional statements.

Link to comment
https://forums.phpfreaks.com/topic/199482-html-parsing-help/#findComment-1047411
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
https://forums.phpfreaks.com/topic/199482-html-parsing-help/#findComment-1047452
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.