How do you parse a file and pull a block of markup? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: How do you parse a file and pull a block of markup? (/showthread.php?tid=18347) Pages:
1
2
|
How do you parse a file and pull a block of markup? - El Forum - 05-03-2009 [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. How do you parse a file and pull a block of markup? - El Forum - 05-03-2009 [eluser]TheFuzzy0ne[/eluser] I think you'd be better off traversing the DOM - http://www.phpro.org/examples/Parse-HTML-With-PHP-And-DOM.html Hope this helps. How do you parse a file and pull a block of markup? - El Forum - 05-04-2009 [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-With-PHP-And-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? How do you parse a file and pull a block of markup? - El Forum - 05-04-2009 [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-With-PHP-And-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 ? How do you parse a file and pull a block of markup? - El Forum - 05-04-2009 [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 How do you parse a file and pull a block of markup? - El Forum - 05-04-2009 [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! How do you parse a file and pull a block of markup? - El Forum - 05-04-2009 [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 How do you parse a file and pull a block of markup? - El Forum - 05-04-2009 [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 ) How do you parse a file and pull a block of markup? - El Forum - 05-04-2009 [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: How do you parse a file and pull a block of markup? - El Forum - 05-04-2009 [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!!! |