2

I need to convert a whois query that returns a simple string to either a PHP object, PHP associative array or a PHP json object.

The text I get returned from the whois server looks like this...

Domain Name: SAILING-WHITSUNDAYS.COM
Registry Domain ID: 95143260_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.wildwestdomains.com
Registrar URL: http://www.wildwestdomains.com
Update Date: 2012-07-24T05:38:29Z
Creation Date: 2003-02-21T05:20:23Z
Registrar Registration Expiration Date: 2021-02-21T05:20:23Z
Registrar: Wild West Domains, LLC
Registrar IANA ID: 440
Registrar Abuse Contact Email: abuse@wildwestdomains.com
Registrar Abuse Contact Phone: +1.480-624-2505
Reseller: Domains Priced Right
Domain Status: clientTransferProhibited http://www.icann.org/epp#clientTransferProhibited
Domain Status: clientUpdateProhibited http://www.icann.org/epp#clientUpdateProhibited
Domain Status: clientRenewProhibited http://www.icann.org/epp#clientRenewProhibited
Domain Status: clientDeleteProhibited http://www.icann.org/epp#clientDeleteProhibited
Registry Registrant ID:
Registrant Name: Craig Hamilton
Registrant Organization:
Registrant Street: Airlie Beach
Registrant City: Airlie Beach
Registrant State/Province: Queensland
Registrant Postal Code: 4802
Registrant Country: Australia
Registrant Phone: 749461451
Registrant Phone Ext:
Registrant Fax:
Registrant Fax Ext:
Registrant Email: craig@mr-travel.com
Registry Admin ID:
Admin Name: Craig Hamilton
Admin Organization:
Admin Street: Airlie Beach
Admin City: Airlie Beach
Admin State/Province: Queensland
Admin Postal Code: 4802
Admin Country: Australia
Admin Phone: 749461451
Admin Phone Ext:
Admin Fax:
Admin Fax Ext:
Admin Email: craig@mr-travel.com
Registry Tech ID:
Tech Name: Craig Hamilton
Tech Organization:
Tech Street: Airlie Beach
Tech City: Airlie Beach
Tech State/Province: Queensland
Tech Postal Code: 4802
Tech Country: Australia
Tech Phone: 749461451
Tech Phone Ext:
Tech Fax:
Tech Fax Ext:
Tech Email: craig@mr-travel.com
Name Server: NS1.OZIDEA.COM
Name Server: NS2.OZIDEA.COM
DNSSEC: unsigned
URL of the ICANN WHOIS Data Problem Reporting System: http://wdprs.internic.net/
Last update of WHOIS database: 2015-05-05T04:00:00Z
For more information on Whois status codes, please visit https://www.icann.org/resources/pages/epp-status-codes-2014-06-16-en
The data contained in this Registrar's Whois database,
while believed by the registrar to be reliable, is provided "as is"
with no guarantee or warranties regarding its accuracy. This information
is provided for the sole purpose of assisting you in obtaining
information about domain name registration records. Any use of
this data for any other purpose is expressly forbidden without
the prior written permission of this registrar.  By submitting an
inquiry, you agree to these terms of usage and limitations of warranty.
In particular, you agree not to use this data to allow, enable, or
otherwise make possible, dissemination or collection of this data, in
part or in its entirety, for any purpose, such as the transmission of
unsolicited advertising and solicitations of any kind, including spam.
You further agree not to use this data to enable high volume, automated
or robotic electronic processes designed to collect or compile this data
for any purpose, including mining this data for your own personal or
commercial purposes.
Please note: the owner of the domain name is specified in the "registrant" section.
In most cases, the Registrar is not the owner of domain names listed in this database.

What I really need though is to extract specific parts of this something like this...

$Result->Reseller;

or

$Result['Reseller'];

Each new field in the string starts with text then a ':' then ends with the value.

Does anyone have a clever plan to be able to parse this string into an object or associative array ?

Craig
  • 387
  • 4
  • 16
  • how did you got this string anyway? from `exec`? – Kevin May 05 '15 at 03:51
  • specifically which parts you want extract? – Adrian Cid Almaguer May 05 '15 at 03:51
  • use a whois api that returns an already formatted string\object\array –  May 05 '15 at 03:53
  • #Ghost - I got the code from http://www.phpeasycode.com/whois/ – Craig May 05 '15 at 04:06
  • #Adrian - I'd like to extract all available fields as an array or object – Craig May 05 '15 at 04:06
  • #Dragon - All the other PHP classes required composer which I am unfamiliar with. This one requires no API account or Composer to use. I just need to be able to extract the data in a more usable fashion from the text string returned. – Craig May 05 '15 at 04:07
  • @Craig it would be much easier if you got this just by the raw string including the newlines, yours right now is just one line string – Kevin May 05 '15 at 04:13
  • Hi @Ghost. The string is currently being output after being returned like this...echo '
    '.htmlspecialchars($whois->data); I'm not quite sure what you mean ?
    – Craig May 05 '15 at 04:16
  • @Craig does that have the newlines on it, should be much easier to disect those values with it – Kevin May 05 '15 at 04:23
  • Sorry @Ghost. The string is being output by this...echo "
    \n" . $result . "\n
    \n"; and It is across multiple lines. Does that help us extract each field and value ? I have edited the initial question to show the actual data string output with line breaks.
    – Craig May 05 '15 at 04:23
  • @Craig can't test what you have right now, but a simple `explode` by newline should give you some start, then have your starting and ending criteria while scanning each exploded line – Kevin May 05 '15 at 04:27
  • Thanks @Ghost, I'll try down that path and see how I go. Thanks for the direction. – Craig May 05 '15 at 04:28

1 Answers1

6

The lines are separated by a newline character, and in every line that has a dictionary pattern, the entries and their corresponding values are separated by the first ':' (other lines are supposed to be complementary information which don't have a dictionary pattern), the following will get you started:

$rows = explode("\n", $strResult);
$arr = array('info'=>"");
foreach($rows as $row) {
    $posOfFirstColon = strpos($row, ":");
    if($posOfFirstColon === FALSE)
        $arr['info'] .= $row;
    else
        $arr[substr($row, 0, $posOfFirstColon)] = trim(substr($row, $posOfFirstColon+1));
}
echo "<pre>";
var_dump($arr);
echo "</pre>";
someOne
  • 1,975
  • 2
  • 14
  • 20
  • Hi someOne. Thanks. I had been going down this path for the last 20 minutes or so based on @Ghost's suggestions. This worked for me. – Craig May 05 '15 at 05:02
  • @Craig Your welcome :), but as I've noted this is just a starting point, you may change it to be more suited to your needs, good luck :) – someOne May 05 '15 at 05:04