php-general Digest 6 Sep 2008 08:34:29 -0000 Issue 5666
Topics (messages 279566 through 279589):
Re: Sending POST variables without html code
279566 by: Robert Cummings
Re: Google Chrome
279567 by: TG
279589 by: Richard Heyes
Sending username/password
279568 by: Jay Moore
279569 by: Robert Cummings
279570 by: Wolf
279571 by: Stut
279572 by: Jay Moore
279573 by: Robert Cummings
279574 by: Jay Moore
279575 by: Stut
279576 by: Jay Moore
279577 by: Robert Cummings
279578 by: Robert Cummings
279579 by: Micah Gersten
279580 by: Jochem Maas
279581 by: Jay Moore
279582 by: Jay Moore
279583 by: Stut
279585 by: Robert Cummings
279586 by: Robert Cummings
279587 by: Robert Cummings
Re: Using DOM textContent Property
279584 by: Tim Gustafson
279588 by: Nathan Nobbe
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[EMAIL PROTECTED]
----------------------------------------------------------------------
--- Begin Message ---
On Fri, 2008-09-05 at 11:40 -0600, R B wrote:
> Hi,
>
> I need to create a cron job PHP script to access every day the information
> of a webpage that is out of my server. This webpage needs POST variables to
> display the information.
>
> How can i send this POST variables from my PHP script without having a html
> form, and receive the required information?
Make a socket connection or preferrably use CURL to do the low level
stuff for you.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
Under the Options and "Under the Hood" there's an option called:
"Use DNS pre-fetching to improve page load performance"
I think that's what I was thinking of. That's probably not pre-fetching
whole pages so much as just resolving DNS ahead of time.
There still could be some pre-caching going on that you just can't control.
Not sure.
-TG
----- Original Message -----
From: "Richard Heyes" <[EMAIL PROTECTED]>
To: TG <[EMAIL PROTECTED]>
Cc: "Ovidiu Rosoiu" <[EMAIL PROTECTED]>, [EMAIL PROTECTED]
Date: Fri, 5 Sep 2008 09:12:13 +0100
Subject: Re: [PHP] Google Chrome
> Hi,
>
> > There's an option for pre-buffering things, I believe.
>
> Where?
>
> --
> Richard Heyes
>
> HTML5 Graphing for IE7, FF, Opera and Safari:
> http://www.phpguru.org/RGraph
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--- End Message ---
--- Begin Message ---
> Under the Options and "Under the Hood" there's an option called:
Nope, no effect I'm afraid.
--
Richard Heyes
HTML5 Graphing for IE7, FF, Opera and Safari:
http://www.phpguru.org/RGraph
--- End Message ---
--- Begin Message ---
Greetings list!
Is it possible (and if so, how) to send username and password
information to a website with PHP?
I would like to submit some information to some network devices we have,
but they require login credentials to proceed. I would like to bypass
the traditional username/password prompt so I can automate the procedure.
I hope this makes sense.
Thanks,
Jay
--- End Message ---
--- Begin Message ---
On Fri, 2008-09-05 at 15:01 -0500, Jay Moore wrote:
> Greetings list!
>
> Is it possible (and if so, how) to send username and password
> information to a website with PHP?
>
> I would like to submit some information to some network devices we have,
> but they require login credentials to proceed. I would like to bypass
> the traditional username/password prompt so I can automate the procedure.
>
> I hope this makes sense.
If you mean http auth style login information then you do the following:
http://www.somedomain.com:[EMAIL PROTECTED]/the/path/to/resource.html
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
Jay Moore wrote:
> Greetings list!
>
> Is it possible (and if so, how) to send username and password
> information to a website with PHP?
>
In one word...
CURL
--- End Message ---
--- Begin Message ---
On 5 Sep 2008, at 21:05, Robert Cummings wrote:
On Fri, 2008-09-05 at 15:01 -0500, Jay Moore wrote:
Greetings list!
Is it possible (and if so, how) to send username and password
information to a website with PHP?
I would like to submit some information to some network devices we
have,
but they require login credentials to proceed. I would like to
bypass
the traditional username/password prompt so I can automate the
procedure.
I hope this makes sense.
If you mean http auth style login information then you do the
following:
http://www.somedomain.com:[EMAIL PROTECTED]/the/path/to/resource.html
I think he meant http://user:[EMAIL PROTECTED]/the/path/to/resource.html
.
Jay: How does the device ask for the username and password? Is it a
form on the web page, a window from the browser, or what?
-Stut
--
http://stut.net/
--- End Message ---
--- Begin Message ---
Stut wrote:
On 5 Sep 2008, at 21:05, Robert Cummings wrote:
On Fri, 2008-09-05 at 15:01 -0500, Jay Moore wrote:
Greetings list!
Is it possible (and if so, how) to send username and password
information to a website with PHP?
I would like to submit some information to some network devices we have,
but they require login credentials to proceed. I would like to bypass
the traditional username/password prompt so I can automate the
procedure.
I hope this makes sense.
If you mean http auth style login information then you do the following:
http://www.somedomain.com:[EMAIL PROTECTED]/the/path/to/resource.html
I think he meant
http://user:[EMAIL PROTECTED]/the/path/to/resource.html.
Jay: How does the device ask for the username and password? Is it a form
on the web page, a window from the browser, or what?
-Stut
Stut -
Standard browser prompt. I'm usually pretty good with PHP stuff and
I've bypassed normal forms many times before, but I've never tried to
bypass the browser popup (.htaccess or similar, I presume?).
Jay
--- End Message ---
--- Begin Message ---
On Fri, 2008-09-05 at 21:07 +0100, Stut wrote:
> On 5 Sep 2008, at 21:05, Robert Cummings wrote:
> > On Fri, 2008-09-05 at 15:01 -0500, Jay Moore wrote:
> >> Greetings list!
> >>
> >> Is it possible (and if so, how) to send username and password
> >> information to a website with PHP?
> >>
> >> I would like to submit some information to some network devices we
> >> have,
> >> but they require login credentials to proceed. I would like to
> >> bypass
> >> the traditional username/password prompt so I can automate the
> >> procedure.
> >>
> >> I hope this makes sense.
> >
> > If you mean http auth style login information then you do the
> > following:
> >
> > http://www.somedomain.com:[EMAIL PROTECTED]/the/path/to/resource.html
>
> I think he meant http://user:[EMAIL PROTECTED]/the/path/to/resource.html
Yeah, that's it lol :)
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
Wolf wrote:
Jay Moore wrote:
Greetings list!
Is it possible (and if so, how) to send username and password
information to a website with PHP?
In one word...
CURL
A couple of people have responded (to me; not the list) with that very
same response. I've heard of it, but never used it before. Care to
give me the 30 second rundown [I can read the site, sure, but it's
probably easier if you explained it :) ]? Is it like a glorified socket
connection or something?
Jay
--- End Message ---
--- Begin Message ---
Jay Moore wrote:
Stut wrote:
On 5 Sep 2008, at 21:05, Robert Cummings wrote:
On Fri, 2008-09-05 at 15:01 -0500, Jay Moore wrote:
Greetings list!
Is it possible (and if so, how) to send username and password
information to a website with PHP?
I would like to submit some information to some network devices we
have,
but they require login credentials to proceed. I would like to bypass
the traditional username/password prompt so I can automate the
procedure.
I hope this makes sense.
If you mean http auth style login information then you do the following:
http://www.somedomain.com:[EMAIL PROTECTED]/the/path/to/resource.html
I think he meant
http://user:[EMAIL PROTECTED]/the/path/to/resource.html.
Jay: How does the device ask for the username and password? Is it a
form on the web page, a window from the browser, or what?
-Stut
Stut -
Standard browser prompt. I'm usually pretty good with PHP stuff and
I've bypassed normal forms many times before, but I've never tried to
bypass the browser popup (.htaccess or similar, I presume?).
The browser popup is caused by a requirement for HTTP authentication.
Curl is your best bet for this. All you need to do is set the following
option with the username and password...
curl_setopt($curl_handle, CURLOPT_USERPWD, $username.':'.$password);
...for each request. If you don't have curl available then you can fall
back on opening a socket and writing the HTTP request manually. If you
need to do this lemme know - I have some code kicking around somewhere
that does it.
-Stut
--
http://stut.net/
--- End Message ---
--- Begin Message ---
Robert Cummings wrote:
On Fri, 2008-09-05 at 21:07 +0100, Stut wrote:
On 5 Sep 2008, at 21:05, Robert Cummings wrote:
On Fri, 2008-09-05 at 15:01 -0500, Jay Moore wrote:
Greetings list!
Is it possible (and if so, how) to send username and password
information to a website with PHP?
I would like to submit some information to some network devices we
have,
but they require login credentials to proceed. I would like to
bypass
the traditional username/password prompt so I can automate the
procedure.
I hope this makes sense.
If you mean http auth style login information then you do the
following:
http://www.somedomain.com:[EMAIL PROTECTED]/the/path/to/resource.html
I think he meant http://user:[EMAIL PROTECTED]/the/path/to/resource.html
Yeah, that's it lol :)
Cheers,
Rob.
FYI - Typing this response on my old 286 because Rob's original
suggestion made my 37.612-core Core2Duo box (used for notepad and
newsgroups only) explode.
Jay
--- End Message ---
--- Begin Message ---
On Fri, 2008-09-05 at 21:24 +0100, Stut wrote:
> Jay Moore wrote:
> > Stut wrote:
> >> On 5 Sep 2008, at 21:05, Robert Cummings wrote:
> >>> On Fri, 2008-09-05 at 15:01 -0500, Jay Moore wrote:
> >>>> Greetings list!
> >>>>
> >>>> Is it possible (and if so, how) to send username and password
> >>>> information to a website with PHP?
> >>>>
> >>>> I would like to submit some information to some network devices we
> >>>> have,
> >>>> but they require login credentials to proceed. I would like to bypass
> >>>> the traditional username/password prompt so I can automate the
> >>>> procedure.
> >>>>
> >>>> I hope this makes sense.
> >>>
> >>> If you mean http auth style login information then you do the following:
> >>>
> >>> http://www.somedomain.com:[EMAIL PROTECTED]/the/path/to/resource.html
> >>
> >> I think he meant
> >> http://user:[EMAIL PROTECTED]/the/path/to/resource.html.
> >>
> >> Jay: How does the device ask for the username and password? Is it a
> >> form on the web page, a window from the browser, or what?
> >>
> >> -Stut
> >>
> >
> > Stut -
> >
> > Standard browser prompt. I'm usually pretty good with PHP stuff and
> > I've bypassed normal forms many times before, but I've never tried to
> > bypass the browser popup (.htaccess or similar, I presume?).
>
> The browser popup is caused by a requirement for HTTP authentication.
> Curl is your best bet for this. All you need to do is set the following
> option with the username and password...
>
> curl_setopt($curl_handle, CURLOPT_USERPWD, $username.':'.$password);
>
> ...for each request. If you don't have curl available then you can fall
> back on opening a socket and writing the HTTP request manually. If you
> need to do this lemme know - I have some code kicking around somewhere
> that does it.
Can also use file() or file_get_contents() if your PHP config enables
allow_url_fopen
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
On Fri, 2008-09-05 at 15:26 -0500, Jay Moore wrote:
> Robert Cummings wrote:
> > On Fri, 2008-09-05 at 21:07 +0100, Stut wrote:
> >> On 5 Sep 2008, at 21:05, Robert Cummings wrote:
> >>> On Fri, 2008-09-05 at 15:01 -0500, Jay Moore wrote:
> >>>> Greetings list!
> >>>>
> >>>> Is it possible (and if so, how) to send username and password
> >>>> information to a website with PHP?
> >>>>
> >>>> I would like to submit some information to some network devices we
> >>>> have,
> >>>> but they require login credentials to proceed. I would like to
> >>>> bypass
> >>>> the traditional username/password prompt so I can automate the
> >>>> procedure.
> >>>>
> >>>> I hope this makes sense.
> >>> If you mean http auth style login information then you do the
> >>> following:
> >>>
> >>> http://www.somedomain.com:[EMAIL PROTECTED]/the/path/to/resource.html
> >> I think he meant http://user:[EMAIL PROTECTED]/the/path/to/resource.html
> >
> > Yeah, that's it lol :)
> >
> > Cheers,
> > Rob.
>
> FYI - Typing this response on my old 286 because Rob's original
> suggestion made my 37.612-core Core2Duo box (used for notepad and
> newsgroups only) explode.
:) Sorry bout that. However, malformed URL bugs that cause your system
to crash can't really be attributed to me ;)
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
Jay Moore wrote:
> Robert Cummings wrote:
>> On Fri, 2008-09-05 at 21:07 +0100, Stut wrote:
>>> On 5 Sep 2008, at 21:05, Robert Cummings wrote:
>>>> On Fri, 2008-09-05 at 15:01 -0500, Jay Moore wrote:
>>>>> Greetings list!
>>>>>
>>>>> Is it possible (and if so, how) to send username and password
>>>>> information to a website with PHP?
>>>>>
>>>>> I would like to submit some information to some network devices
>>>>> we have,
>>>>> but they require login credentials to proceed. I would like to
>>>>> bypass
>>>>> the traditional username/password prompt so I can automate the
>>>>> procedure.
>>>>>
>>>>> I hope this makes sense.
>>>> If you mean http auth style login information then you do the
>>>> following:
>>>>
>>>> http://www.somedomain.com:[EMAIL PROTECTED]/the/path/to/resource.html
>>> I think he meant
>>> http://user:[EMAIL PROTECTED]/the/path/to/resource.html
>>
>> Yeah, that's it lol :)
>>
>> Cheers,
>> Rob.
>
> FYI - Typing this response on my old 286 because Rob's original
> suggestion made my 37.612-core Core2Duo box (used for notepad and
> newsgroups only) explode.
>
> Jay
>
That'll teach you to use Google Chrome. ;)
Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com
--- End Message ---
--- Begin Message ---
Jay Moore schreef:
Wolf wrote:
Jay Moore wrote:
Greetings list!
Is it possible (and if so, how) to send username and password
information to a website with PHP?
In one word...
CURL
A couple of people have responded (to me; not the list) with that very
same response. I've heard of it, but never used it before. Care to
give me the 30 second rundown [I can read the site, sure, but it's
probably easier if you explained it :) ]? Is it like a glorified socket
connection or something?
is it too much to ask to spend 30 seconds RTFM? http://php.net/curl
(it's an extension based on libcurl if you care to google further)
cURL = Client URL Libary ... read it really really slowly, then it may take 30
seconds.
Jay
--- End Message ---
--- Begin Message ---
:) Sorry bout that. However, malformed URL bugs that cause your system
to crash can't really be attributed to me ;)
Cheers,
Rob.
The prompt that showed before my computer self-destructed referenced you
specifically.
Expect the invoice for my new quantum computer to come in the mail
shortly. I trust you will remit payment promptly. I'd hate to have to
get the internet police involved. I hear they're itchin' to taze you, bro.
Jay
--- End Message ---
--- Begin Message ---
That'll teach you to use Google Chrome. ;)
Pshaw. IE5 4 lyfe, yo.
--- End Message ---
--- Begin Message ---
Robert Cummings wrote:
On Fri, 2008-09-05 at 21:24 +0100, Stut wrote:
Jay Moore wrote:
Stut wrote:
On 5 Sep 2008, at 21:05, Robert Cummings wrote:
On Fri, 2008-09-05 at 15:01 -0500, Jay Moore wrote:
Greetings list!
Is it possible (and if so, how) to send username and password
information to a website with PHP?
I would like to submit some information to some network devices we
have,
but they require login credentials to proceed. I would like to bypass
the traditional username/password prompt so I can automate the
procedure.
I hope this makes sense.
If you mean http auth style login information then you do the following:
http://www.somedomain.com:[EMAIL PROTECTED]/the/path/to/resource.html
I think he meant
http://user:[EMAIL PROTECTED]/the/path/to/resource.html.
Jay: How does the device ask for the username and password? Is it a
form on the web page, a window from the browser, or what?
-Stut
Stut -
Standard browser prompt. I'm usually pretty good with PHP stuff and
I've bypassed normal forms many times before, but I've never tried to
bypass the browser popup (.htaccess or similar, I presume?).
The browser popup is caused by a requirement for HTTP authentication.
Curl is your best bet for this. All you need to do is set the following
option with the username and password...
curl_setopt($curl_handle, CURLOPT_USERPWD, $username.':'.$password);
...for each request. If you don't have curl available then you can fall
back on opening a socket and writing the HTTP request manually. If you
need to do this lemme know - I have some code kicking around somewhere
that does it.
Can also use file() or file_get_contents() if your PHP config enables
allow_url_fopen
Quite correct, assuming it's a GET request that's needed.
-Stut
--
http://stut.net/
--- End Message ---
--- Begin Message ---
On Fri, 2008-09-05 at 15:37 -0500, Jay Moore wrote:
> > :) Sorry bout that. However, malformed URL bugs that cause your system
> > to crash can't really be attributed to me ;)
> >
> > Cheers,
> > Rob.
>
>
> The prompt that showed before my computer self-destructed referenced you
> specifically.
>
> Expect the invoice for my new quantum computer to come in the mail
> shortly. I trust you will remit payment promptly. I'd hate to have to
> get the internet police involved. I hear they're itchin' to taze you, bro.
I sent your payment via quantum mail... let me know if it gets entangled
along the way.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
On Fri, 2008-09-05 at 15:39 -0500, Jay Moore wrote:
> > That'll teach you to use Google Chrome. ;)
>
> Pshaw. IE5 4 lyfe, yo.
I have IE5 running in a vmware appliance. I check sites in it once in a
while for kicks :)
I also have IE 3 running and Netscape 4. It's amazing how ugly the new
tech looks when you go retro... though still looks good if you go retro
to wget ;)
I should track down a copy of mosaic for giggles.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
On Fri, 2008-09-05 at 17:45 -0400, Robert Cummings wrote:
> On Fri, 2008-09-05 at 15:39 -0500, Jay Moore wrote:
> > > That'll teach you to use Google Chrome. ;)
> >
> > Pshaw. IE5 4 lyfe, yo.
>
> I have IE5 running in a vmware appliance. I check sites in it once in a
> while for kicks :)
>
> I also have IE 3 running and Netscape 4. It's amazing how ugly the new
> tech looks when you go retro... though still looks good if you go retro
> to wget ;)
Err, I meant retro to links or lynx.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
Nathan,
Thanks for the suggestion, but it's still not working for me. Here's my
code:
===========
$HTML = new DOMDocument();
@$HTML->loadHTML($text);
$Elements = $HTML->getElementsByTagName("*");
for ($X = 0; $X < $Elements->length; $X++) {
$Element = $Elements->item($X);
if ($Element->tagName == "a") {
# SNIP - Do something with A tags here
} else if ($Element instanceof DOMText) {
echo $Element->nodeValue; exit;
}
}
===========
This loop never executes the instanceof part of the code. If I add:
} else if ($Element instanceof DOMNode) {
echo "foo!"; exit;
}
Then it echos "foo!" as expected. It just seems that none of the nodes in
the tree are DOMText nodes. In fact, get_class($Element) returns
"DOMElement" for every node in the tree.
Tim Gustafson
SOE Webmaster
UC Santa Cruz
[EMAIL PROTECTED]
831-459-5354
________________________________
From: Nathan Nobbe [mailto:[EMAIL PROTECTED]
Sent: Wednesday, September 03, 2008 11:55 AM
To: Tim Gustafson
Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: Re: [PHP] Using DOM textContent Property
On Wed, Sep 3, 2008 at 10:03 AM, Tim Gustafson <[EMAIL PROTECTED]>
wrote:
> I think you might be better off using regexp on the text
> *before* sending it through the DOM parser. Send the
> user's text through a function that searches for URLs
> and email addresses, creating proper links as they're
> found, then use the output from that to move on to your
> DOM stuff. That way, you need not create new nodes in
> your nodelist.
I think that's the way I'm going to have to go, but I was
really hoping not
to. Thanks for the suggestion!
i think i have what youre looking for Tim, take a look at this
script output
[EMAIL PROTECTED] ~ $ php testDom.php
IN:
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>Test<br/><h2>[EMAIL PROTECTED]<a name="bar">stuff
inside the link</a>Foo</h2><p>care</p><p>yoyser</p></body></html>
OUT:
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>Test<br/><h2><a
href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a><a
name="bar">stuff inside the
link</a>Foo</h2><p>care</p><p>yoyser</p></body></html>
and heres the code using the DOM extension
you may have to tweak it to suit your needs, but currently i think
it does the trick ;)
<?php
$doc = new DOMDocument();
$doc->loadHTML('<html><body>Test<br><h2>[EMAIL PROTECTED]<a
name="bar">stuff inside the
link</a>Foo</h2><p>care</p><p>yoyser</p></body></html>');
echo 'IN:' . PHP_EOL . $doc->saveXML() . PHP_EOL;
findTextNodes($doc->getElementsByTagName('*'),
'convertToLinkIfNecc');
echo 'OUT: ' . PHP_EOL . $doc->saveXML() . PHP_EOL;
/**
* run through a DOMNodeList, looking for text nodes. apply a
callback to
* all such text nodes that are encountered
*/
function findTextNodes(DOMNodeList $nodesToSearch, $callback) {
foreach($nodesToSearch as $curNode) {
if($curNode->hasChildNodes())
foreach($curNode->childNodes as $curChild)
if($curChild instanceof DOMText)
#echo "TEXT NODE FOUND: " . $curChild->nodeValue
. PHP_EOL;
/// todo: allow use of hook here
call_user_func($callback, $curNode, $curChild);
}
}
/**
* determine if a node should be modified, by chcking to see if a
child is a text node
* and the text looks like an email address.
* call a subordinate function to convert the text node into a
mailto anchor DOMElement
*/
function convertToLinkIfNecc(DomElement $textContainer, DOMText
$textNode) {
if( (strtolower($textContainer->nodeName) != 'a') &&
(filter_var($textNode->nodeValue, FILTER_VALIDATE_EMAIL) !==
false) ) {
convertMailtoToAnchor($textContainer, $textNode);
}
}
/**
* modify a DOMElement that has a DOMText node as a child; create a
DOMElement
* that represents and a tag, and set the value and href attirbute,
so that it
* acts as a 'mailto' link
*/
function convertMailtoToAnchor(DomElement $textContainer, DOMText
$textNode) {
$newNode = new DomElement('a', $textNode->nodeValue);
$textContainer->replaceChild($newNode, $textNode);
$newNode->setAttribute('href', "mailto:{$textNode->nodeValue}");
}
-nathan
--- End Message ---
--- Begin Message ---
bouncing back to the list so that others may benefit from our work...
On Fri, Sep 5, 2008 at 3:09 PM, Tim Gustafson <[EMAIL PROTECTED]> wrote:
> Nathan,
>
> Thanks for the suggestion, but it's still not working for me. Here's my
> code:
>
> ===========
> $HTML = new DOMDocument();
> @$HTML->loadHTML($text);
> $Elements = $HTML->getElementsByTagName("*");
>
> for ($X = 0; $X < $Elements->length; $X++) {
> $Element = $Elements->item($X);
>
> if ($Element->tagName == "a") {
> # SNIP - Do something with A tags here
> } else if ($Element instanceof DOMText) {
> echo $Element->nodeValue; exit;
> }
> }
> ===========
>
> This loop never executes the instanceof part of the code. If I add:
>
> } else if ($Element instanceof DOMNode) {
> echo "foo!"; exit;
> }
>
> Then it echos "foo!" as expected. It just seems that none of the nodes in
> the tree are DOMText nodes. In fact, get_class($Element) returns
> "DOMElement" for every node in the tree.
Tim,
i got your code working with minimal effort by pulling in two of the methods
i posted and making some revisions. scope it out,
(this will produce the same output as my last post (the part after OUT:))
<?php
$text = '<html><body>Test<br><h2>[EMAIL PROTECTED]<a name="bar">stuff
inside the link</a>Foo</h2><p>care</p><p>yoyser</p></body></html>';
$HTML = new DOMDocument();
$HTML->loadHTML($text);
$Elements = $HTML->getElementsByTagName("*");
for ($X = 0; $X < $Elements->length; $X++) {
$Element = $Elements->item($X);
if($Element->hasChildNodes())
foreach($Element->childNodes as $curChild)
if ($curChild->nodeName == "a") {
# SNIP - Do something with A tags here
} else if ($curChild instanceof DOMText) {
convertToLinkIfNecc($Element, $curChild);
}
}
echo $HTML->saveXML() . PHP_EOL;
function convertToLinkIfNecc(DomElement $textContainer, DOMText $textNode) {
if( (strtolower($textContainer->nodeName) != 'a') &&
(filter_var($textNode->nodeValue, FILTER_VALIDATE_EMAIL) !== false)
) {
convertMailtoToAnchor($textContainer, $textNode);
}
}
function convertMailtoToAnchor(DomElement $textContainer, DOMText $textNode)
{
$newNode = new DomElement('a', $textNode->nodeValue);
$textContainer->replaceChild($newNode, $textNode);
$newNode->setAttribute('href', "mailto:{$textNode->nodeValue}");
}
?>
so, the problem is iterating over a tree structure will only show you whats
at the first level of the tree. this is why you need to call
hasChildNodes(), and if that is true, call childNodes() and iterate across
that (and really, the code should be doing the same thing there as well,
calling hasChildNodes() and iterating over the results of childNodes()).
the code i have shown will work for the html i posted, however it wont work
on (x)html where these text nodes we're searching for are deeper in the tree
than the second level. im sure you can cook up something that will recurse
down to the leafs :)
anyway, im going to try and hook up a RecursiveDOMDocumentIterator that
implements RecursiveIterator so that it has the convenient foreach support.
also, ill probly try to hook up a Filter varient of this class so that
situations like this are trivial.
stay tuned :D
-nathan
--- End Message ---