• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you parse a file and pull a block of markup?

#1
[eluser]nubianxp[/eluser]
I have a remote file for example, which contains the ff:
Code:
...bunch of markups...
<div id="block-i-need-to-get">
  <div>some content</div>
  <div>some more content</div>
</div>
...more 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.

#2
[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.

#3
[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?

#4
[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 ?

#5
[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

#6
[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!

#7
[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 Smile

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) {
            //De auteur
            $author     = $template_infos->getElementsByTagName('author');
              $author        = $author->item(0)->nodeValue;
            //De naam van het thema
            $name         = $template_infos->getElementsByTagName('name');
              $name        = $name->item(0)->nodeValue;
            //De website van de eigenaar van het thema
            $website     = $template_infos->getElementsByTagName('website');
              $website    = $website->item(0)->nodeValue;
            //De copyrights
            $copyright     = $template_infos->getElementsByTagName('copyright');
              $copyright    = $copyright->item(0)->nodeValue;
            //De licentie van het thema
            $license     = $template_infos->getElementsByTagName('license');
              $license     = $license ->item(0)->nodeValue;
            //De beschrijving
            $desc         = $template_infos->getElementsByTagName('description');
              $desc         = $desc ->item(0)->nodeValue;            
        }

I used that code to parse an .xml file which would contain info about a template Smile

#8
[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 Tongue)

#9
[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';
$DOM = new DOMDocument();
$DOM->load($remote);
$content = $DOM->getElementById('somediv');
and do either
Code:
echo $content; OR
print_r($content);

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:

#10
[eluser]Dam1an[/eluser]
@nubianxp: Yeah they're all the built in PHP functions, then there's another million extra user created functions Tongue
PHP has grown to be huge!!!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.