Mike088 Posted April 23, 2010 Share Posted April 23, 2010 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 More sharing options...
JAY6390 Posted April 23, 2010 Share Posted April 23, 2010 I don't think it's actually possible to do this unless you have the values within a node Link to comment https://forums.phpfreaks.com/topic/199482-html-parsing-help/#findComment-1046963 Share on other sites More sharing options...
Mike088 Posted April 23, 2010 Author Share Posted April 23, 2010 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 More sharing options...
Mike088 Posted April 23, 2010 Author Share Posted April 23, 2010 Anyone else have any suggestions on this? Link to comment https://forums.phpfreaks.com/topic/199482-html-parsing-help/#findComment-1047408 Share on other sites More sharing options...
mrMarcus Posted April 23, 2010 Share Posted April 23, 2010 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 More sharing options...
Mike088 Posted April 24, 2010 Author Share Posted April 24, 2010 Thanks mrMarcus that 'file_get_contents' does the trick well and overall makes everything alot easier too 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.