PHP code example of druc / xdt-parser

1. Go to this page and download the library: Download druc/xdt-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/ */

    

druc / xdt-parser example snippets



use Druc\XdtParser\XdtParser;

// Create instance
$this->parser = XdtParser::make(
    file_get_contents(__DIR__ . '/data/sample.ldt'), // file contents 
    ['id' => '8316', 'observation' => '6220'] // field mapping
);

// Get first value matching the id field
$this->parser->first('id'); // --> '123'

// Get all values matching the observation field
$this->parser->find('observation'); // --> ['observation 1', 'observation 2'];

// Also works with the xdt code
$this->parser->first('8316'); // --> '123'
$this->parser->find('6220'); // --> ['observation 1', 'observation 2'];

// Get mapped values
$this->parser->getMapped(); // -> ['id' => 123, 'observation' => ['observation 1', 'observation 2']];

// Add extra field mapping
$this->parser->addFieldsMap(['my_value' => '3213']);

// Remove fields
$this->parser->removeFields(['id']);

// Get all values (mapped and unkown/unmapped values)
$this->parser->all(); // -> ['3213' => 'unkown code value', 'id' => 123, 'observation' => ['observation 1', 'observation 2']];

// Get field key
$this->parser->getKey('observation'); // -> 6220 if the mapping contains 'observation' => '6220'

// Get field name
$this->parser->getFieldName('6220'); // -> `observation` if the mapping contains 'observation' => '6220'

// Get xdtRows
$this->parser->getXdtRows(); // will return an array with the unparsed rows of your content: ['0346220Dies ist ein zweizeiliger', '0143102Frank']

// Parse single string
$this->parser->parseSingle('0346220Dies ist ein zweizeiliger'); // -> ['length' => 32, field = '6220', 'value' => 'Dies ist ein zweizeiliger'];