|
-
Feb 22nd, 2007, 09:28 PM
#1
Thread Starter
Frenzied Member
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.
-
Feb 22nd, 2007, 09:38 PM
#2
Re: Parsing tags.
PHP Code:
$doc = new DOMDocument();
$doc->loadHTMLFile($filename);
$imgs = $doc->getElementsByTagName('img');
for ($imgs as $img)
echo $img->getAttribute('src')."\n";
-
Feb 22nd, 2007, 09:48 PM
#3
Thread Starter
Frenzied Member
Re: Parsing tags.
 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
-
Feb 23rd, 2007, 01:16 AM
#4
-
Feb 23rd, 2007, 12:35 PM
#5
Thread Starter
Frenzied Member
Re: Parsing tags.
 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.
-
Feb 23rd, 2007, 02:36 PM
#6
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.
-
Feb 23rd, 2007, 02:43 PM
#7
Thread Starter
Frenzied Member
Re: Parsing tags.
 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.
-
Feb 23rd, 2007, 03:01 PM
#8
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.
-
Feb 23rd, 2007, 05:50 PM
#9
Thread Starter
Frenzied Member
Re: Parsing tags.
 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.
-
Mar 1st, 2007, 02:02 AM
#10
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?
-
Mar 1st, 2007, 07:43 PM
#11
Thread Starter
Frenzied Member
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.
-
Mar 2nd, 2007, 02:53 AM
#12
Re: Parsing tags.
 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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|