Re: My first PHP Script... [message #175798 is a reply to message #175797] |
Thu, 27 October 2011 01:00 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
On Wed, 26 Oct 2011 16:26:41 -0700, SpreadTooThin wrote:
> Hi, I'm a seasoned programmer that needs to do this one program and then
> I'll probably never program in PHP for another 5 years....
>
> I have a perl script that was created by another programmer, who is long
> gone...
> I need to take the output of that script and then 'massage' it so that
> the data can be formated and displayed to my liking..
>
> The script is
>
> http://myserver/cgi-bin/path/deptlist.pl?dept=HR
>
> This script gives me a phone list of all the people in HR except the
> color coding is terrible.
> So pardon my ignorance here but this is where I was told to start...
>
> <?php
> include_once "deptlist.pl";
> ?>
Doesn't sound right
What you may want to do, depending on whether you want a string, an
array, or a tree of nodes, is:
<?php
$str = file_get_contents("http://myserver/cgi-bin/path/deptlist.pl?
dept=HR");
// $str contains the whole output of the perl script including any
linebreaks
?>
<?php
$ary = file("http://myserver/cgi-bin/path/deptlist.pl?
dept=HR",FILE_IGNORE_NEW_LINES);
// $ary contains the whole output of the perl script as an array of lines
(without terminating linebreaks)
?>
<?php
$doc = new DOMDocument();
$doc->loadHTMLFile("http://myserver/cgi-bin/path/deptlist.pl?dept=HR");
// $doc is the root of a tree of nodes that represents the html document
generated by the perl script
$body = $doc->getElementsByTagName('body')->item(0);
// $body is the root of a tree of nodes that represents the html document
<body>contents</body> element generated by the perl script
// If the body contains tables, name in cell 0, room number in cell 1,
ext in cell 2, and you want name and ext:
$rows = $body->getElementsByTagName('tr');
for ($iR = 0; $iR < $rows->length; $iR++)
{
$cells = $rows->item($iR)->getElementsByTagName('td');
$name = $cells->item(0)->textContent;
$ext = $cells->item(2)->textContent;
file_put_contents ("new_file.txt","{$name} {$ext}\n",FILE_APPEND);
}
// if the body contains lists and you just want the content of the "li"
elements:
$items = $body->getElementsByTagName('li');
for ($iR = 0; $iR < $rows->length; $iR++)
{
$data = $items->item($iR)->textContent;
file_put_contents ("new_file.txt","{$data}\n",FILE_APPEND);
}
// etc
?>
Opening a url with the file open commands depends on the relevant ini
value being set to enable this, however I believe it is the default
setting.
If the perl script generates reasonably well structured html, it may be
easier to manipulate and output it using the domdocument methods.
Rgds
Denis McMahon
|
|
|