My first PHP Script... [message #175797] |
Wed, 26 October 2011 19:26 |
|
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";
?>
Not much to go on, but is there a way to get the output of the script
into a string variable and then parse the string and produce the
output to my liking?
Should i be attempting this in PHP or should I perhaps try java
script?
Get me started and I'll figure out the rest....
Thanks.
|
|
|
Re: My first PHP Script... [message #175798 is a reply to message #175797] |
Wed, 26 October 2011 21:00 |
|
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
|
|
|
|
Re: My first PHP Script... [message #175802 is a reply to message #175798] |
Wed, 26 October 2011 23:31 |
|
> If the perl script generates reasonably well structured html, it may be
> easier to manipulate and output it using the domdocument methods.
>
I wish I could just get the source of the pearl script and modify
that...
|
|
|
|