PHP code example of dcarbone / tns-parser

1. Go to this page and download the library: Download dcarbone/tns-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dcarbone / tns-parser example snippets


$tns = <<<STRING
AWESOME.MYSELF.DATABASE =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS =
                (PROTOCOL = TCP)
                (HOST = me.db.myself.net)
                (PORT = 12345)
            )
        )
        (CONNECT_DATA =
            (SID = AWESOME)
            (SERVER = dedicated)
        )
    )

#--------------------------------------------------

AWESOME2.MYSELF.DATABASE =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS =
                (PROTOCOL = TCP)
                (HOST = me2.db.myself.net)
                (PORT = 12345)
            )
            (ADDRESS =
                (PROTOCOL = TCP)
                (HOST = me25.db.myself.net)
                (PORT = 12345)
            )
            (LOAD_BALANCE = on)
            (FAILOVER = on)
            (ENABLE = broken)
        )
        (CONNECT_DATA =
            (SID = AWESOME2)
            (SERVER = dedicated)
            (FAILOVER_MODE =
                (TYPE = select)
                (METHOD = basic)
                (RETRIES = 120)
                (DELAY = 2)
            )
        )
    )
STRING;


$parser = new \DCarbone\TNSParser();

$parser->parseFile('path-to-file');

$parser->parseString($tns);

$matched = $parser->search('awesome');

var_export($matched);
/*
array (
  0 => 'AWESOME.MYSELF.DATABASE',
  1 => 'AWESOME2.MYSELF.DATABASE',
)
*/

$matched = $parser->search('me2.db.myself');
var_export($matched):
/*
array (
  0 => 'AWESOME2.MYSELF.DATABASE',
)
*/

// Using the 2nd match statement...
$entries = array();
foreach($matched as $name)
{
    $entries = $parser[$name];
}
var_export($entries);
/*
array (
  'DESCRIPTION' =>
  array (
    'ADDRESS_LIST' =>
    array (
      'ADDRESS' =>
      array (
        0 =>
        array (
          'PROTOCOL' => 'TCP',
          'HOST' => 'me2.db.myself.net',
          'PORT' => '12345',
        ),
        1 =>
        array (
          'PROTOCOL' => 'TCP',
          'HOST' => 'me25.db.myself.net',
          'PORT' => '12345',
        ),
      ),
      'LOAD_BALANCE' => 'on',
      'FAILOVER' => 'on',
      'ENABLE' => 'broken',
    ),
    'CONNECT_DATA' =>
    array (
      'SID' => 'AWESOME2',
      'SERVER' => 'dedicated',
      'FAILOVER_MODE' =>
      array (
        'TYPE' => 'select',
        'METHOD' => 'basic',
        'RETRIES' => '120',
        'DELAY' => '2',
      ),
    ),
  ),
)
*/

$entry = $parser->getTNSEntryString($matched[0]);
var_export($entry);
/*
'AWESOME2.MYSELF.DATABASE =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS =
                (PROTOCOL = TCP)
                (HOST = me2.db.myself.net)
                (PORT = 12345)
            )
            (ADDRESS =
                (PROTOCOL = TCP)
                (HOST = me25.db.myself.net)
                (PORT = 12345)
            )
            (LOAD_BALANCE = on)
            (FAILOVER = on)
            (ENABLE = broken)
        )
        (CONNECT_DATA =
            (SID = AWESOME2)
            (SERVER = dedicated)
            (FAILOVER_MODE =
                (TYPE = select)
                (METHOD = basic)
                (RETRIES = 120)
                (DELAY = 2)
            )
        )
    )'
*/

$parser->sort();