Formatting the output of a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grunshaw
Forum Newbie
Posts: 3
Joined: Tue Feb 03, 2009 5:44 am

Formatting the output of a variable

Post by grunshaw »

Hi guys,
I would like a little bit of help with formatting the output of a variable. This is just to further my knowledge as I am learning PHP and I just like to build things!

Its a Whois script - you can see it working here http://grunshaw.net/thumb/. As you can see, the output of the whois at the bottom is all run together.

The whois part of the code is:

Code: Select all

 
<?php
$domain = $_REQUEST['url'];
$output = shell_exec("whois $domain");
echo $output;
?>
 
What I would like to do is format the variable $output.

If someone can help me, that would be great.

Best
James.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Formatting the output of a variable

Post by mickeyunderscore »

Could you be more specific about the way in which you want to format it?
grunshaw
Forum Newbie
Posts: 3
Joined: Tue Feb 03, 2009 5:44 am

Re: Formatting the output of a variable

Post by grunshaw »

Hi,

I want to be able to find the nameservers and registrant information and may assign them to their own variables - but I generally want to format it like most Whois lookups are formatted, rather than just one huge clump.

Thanks.
James.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Formatting the output of a variable

Post by mickeyunderscore »

The only way I can think of doing this at the moment is quite messy:

Code: Select all

$search = array('Domain name:', 'Registrant:', 'Registrant type:');
$replace = array('#delimiter#Domain name:', '#delimiter#Registrant:', '#delimiter#Registrant type:');
$output = str_replace($search, $replace, $output);
$chunks = explode('#delimiter#', $output);
 
echo $chunks[0] . '<br />';
echo $chunks[1] . '<br />';
echo $chunks[2] . '<br />';
The only thing I could think of was to insert delimiters which you can then explode on. So now $chunks[1] contains the domain name, $chunks[2] contains the registrant information.

There must be a better way than this though, if anyone knows of one?
grunshaw
Forum Newbie
Posts: 3
Joined: Tue Feb 03, 2009 5:44 am

Re: Formatting the output of a variable

Post by grunshaw »

Hi there,

Thanks for the response - you've actually helped me out with another problem I had with searching for specific things and displaying them, so its great.

I have found a very simple solution that I will post here for everyone else to see. You will be suprised how simple this really is.

This is the code to perfomr the Whois lookup and put the data into the variable $output

Code: Select all

<?
$domain = $_REQUEST['url'];
$output = shell_exec("whois $domain");
?>
And here is the code to format it and output it in a normal Whois style so to speak.

Code: Select all

<pre><? echo $output ?></pre>
or if your still within your <? ?> tags

Code: Select all

echo "<pre>$output</pre>";
I hope this is of use to someone as it has been to me and thanks again for the help.
Post Reply