How do you parse a file and pull a block of markup? |
[eluser]nubianxp[/eluser]
I have a remote file for example, which contains the ff: Code: ...bunch of markups... what i need is that particular code block only, and i understand that there's a preg_match() function that can be used for this, the problem is that, i don't know how to construct the code that will do it... any tips/links/snippets? thanks in advance.
[eluser]TheFuzzy0ne[/eluser]
I think you'd be better off traversing the DOM - http://www.phpro.org/examples/Parse-HTML...d-DOM.html Hope this helps.
[eluser]nubianxp[/eluser]
[quote author="TheFuzzy0ne" date="1241415860"]I think you'd be better off traversing the DOM - http://www.phpro.org/examples/Parse-HTML...d-DOM.html Hope this helps.[/quote] Helps a lot! Thank you, and that link is chock-full of PHP goodness! :cheese: BTW, how come codeigniter doesn't have a library/helper for this?
[eluser]Yorick Peterse[/eluser]
[quote author="nubianxp" date="1241440309"][quote author="TheFuzzy0ne" date="1241415860"]I think you'd be better off traversing the DOM - http://www.phpro.org/examples/Parse-HTML...d-DOM.html Hope this helps.[/quote] Helps a lot! Thank you, and that link is chock-full of PHP goodness! :cheese: BTW, how come codeigniter doesn't have a library/helper for this?[/quote] Because the DOM is built in into PHP, so why make an library for it that already exists ?
[eluser]Dam1an[/eluser]
I think he was maybe refering more to a wrapper class... although it looks so easy to use, there probably isn't any need
[eluser]nubianxp[/eluser]
[quote author="Yorick Peterse" date="1241451909"] Because the DOM is built in into PHP, so why make an library for it that already exists ?[/quote] eh? i really didn't know that, guess that shows how noob i am about PHP... :lol: thank you for that info!
[eluser]Yorick Peterse[/eluser]
[quote author="nubianxp" date="1241452674"][quote author="Yorick Peterse" date="1241451909"] Because the DOM is built in into PHP, so why make an library for it that already exists ?[/quote] eh? i really didn't know that, guess that shows how noob i am about PHP... :lol: thank you for that info![/quote] Haha, it's really easy to use, you can parse HTML, XML etc Working with the DOM is really simple (in this case we use an XML file): Initialize it: Code: $DOM = new DOMDocument(); Load a file Code: $DOM->load("yourfile.xml"); Get a certain XML element with the name 'template_info' Code: $template_info = $DOM->getElementsByTagName("template_info"); Loop through each item Code: foreach($template_info as $template_infos) { I used that code to parse an .xml file which would contain info about a template
[eluser]Dam1an[/eluser]
Don't worry, it doesn;t make you a n00b lol With 5344 documented PHP functions no one is expected to know them all (and no, I didn't count them all manually )
[eluser]nubianxp[/eluser]
@yorick: thanks for the code man, nice to have something to start with... one question, is it possible to maybe parse a remote file? e.g. Code: $remote = 'http://example.com/data.html'; Code: echo $content; OR i tested the above, but i get some parsing errors when using $dom->load(): Code: Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: error parsing attribute name in http://example.com/forecast.html, line: 19 in E:\localweb\projects\test\getfile.php on line 5 and a blank page if i use $dom->loadHTML()... anyway, appreciate the help. :lol: @dam1an: 5,344?! and all of those are built-in?! :wow:
[eluser]Dam1an[/eluser]
@nubianxp: Yeah they're all the built in PHP functions, then there's another million extra user created functions PHP has grown to be huge!!! |
Welcome Guest, Not a member yet? Register Sign In |