Results 1 to 12 of 12

Thread: Parsing tags.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Parsing tags.

    Okay can someone help me, I can't find a good example of parsing tags. I wanna take and get all the <img> tags in a file on my server.

    Code:
    //more stuff here
    <img class="inlineimg" src="blak_ice/statusicon/post_new.gif" alt="Unread" border="0" />
    /more stuff here
    I wanna in the end have an array with all the img tags and get the src part of them. Can anyone help?
    Last edited by high6; Feb 23rd, 2007 at 06:04 PM.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Parsing tags.

    PHP Code:
    $doc = new DOMDocument();
    $doc->loadHTMLFile($filename);
    $imgs $doc->getElementsByTagName('img');
    for (
    $imgs as $img)
      echo 
    $img->getAttribute('src')."\n"

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Parsing tags.

    Quote Originally Posted by penagate
    PHP Code:
    $doc = new DOMDocument();
    $doc->loadHTMLFile($filename);
    $imgs $doc->getElementsByTagName('img');
    for (
    $imgs as $img)
      echo 
    $img->getAttribute('src')."\n"
    Code:
    Parse error: syntax error, unexpected T_AS, expecting ';' in /home/www/test.php on line 11

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Parsing tags.

    foreach

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Parsing tags.

    Quote Originally Posted by penagate
    foreach
    Also wouldn't It need the {}?

    Also it errors.

    Fatal error: Cannot instantiate non-existent class: domdocument in /home/www/test.php on line 8

    which is

    $doc = new DOMDocument();
    Last edited by high6; Feb 23rd, 2007 at 12:46 PM.

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Parsing tags.

    you don't need the curly brackets ("{" and "}") if it's a one line statement, that's why this works:
    PHP Code:
    foreach(statement)
      
    line

      
    //while this wouldn't work if there were multiple lines to execute, so you use:
      
    foreach(statement){
        
    line1
        line2
        line3
      

    about the DOMDocument; the only thing I can guess is that it requires PHP5.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Parsing tags.

    Quote Originally Posted by kows
    you don't need the curly brackets ("{" and "}") if it's a one line statement, that's why this works:
    PHP Code:
    foreach(statement)
      
    line

      
    //while this wouldn't work if there were multiple lines to execute, so you use:
      
    foreach(statement){
        
    line1
        line2
        line3
      

    about the DOMDocument; the only thing I can guess is that it requires PHP5.
    Server has the newest php version though.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Parsing tags.

    With DOM support? What does phpinfo() say?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Parsing tags.

    Quote Originally Posted by CornedBee
    With DOM support? What does phpinfo() say?
    Site i was using didnt have it. But my secondary site does.

    $doc->loadHTMLFile($filename);

    Is there a way that I can provide the content instead of the filename?
    nm looked here. http://us3.php.net/manual/en/ref.dom.php

    EDIT: Didn't echo anything when I ran it.
    Last edited by high6; Feb 23rd, 2007 at 06:05 PM.

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Parsing tags.

    You document obviously has no images. Or your code has a bug. How can we tell if we cannot see it though?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Parsing tags.

    Actually domdocument only works if its <img src"bla"> </img> but Its <img src"bla"/>. Anyways I just made my own parser.

  12. #12
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Parsing tags.

    Quote Originally Posted by high6
    Actually domdocument only works if its <img src"bla"> </img> but Its <img src"bla"/>. Anyways I just made my own parser.
    No it doesn't. Where is your code?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width