PHP code example of laravel-liberu / php-gedcom

1. Go to this page and download the library: Download laravel-liberu/php-gedcom 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/ */

    

laravel-liberu / php-gedcom example snippets


spl_autoload_register(function ($class) {
    $pathToGedcom = __DIR__ . '/library/'; // TODO FIXME

    if (!substr(ltrim($class, '\\'), 0, 7) == 'Gedcom\\') {
        return;
    }

    $class = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
    if (file_exists($pathToGedcom . $class)) {
        

use Gedcom\GedcomResource;

// Enable caching with custom configuration
$resource = new GedcomResource(
    cacheEnabled: true,
    cacheConfig: [
        'memory_items' => 2000,           // Max items in memory cache
        'cache_dir' => '/tmp/gedcom',     // Cache directory
        'ttl' => 7200                     // Cache TTL in seconds
    ]
);

// Get cache statistics
$stats = $resource->getCacheStats();
echo "Memory items: " . $stats['memory_items'] . "\n";

// Clear cache when needed
$resource->clearCache();

$parser = new \Gedcom\Parser();

// Parse a GEDCOM 5.5.1 file
$gedcom551 = $parser->parse('family_tree_551.ged');

// Parse a GEDCOM 7.0 file
$gedcom70 = $parser->parse('family_tree_70.ged');

// Check the version
$head = $gedcom70->getHead();
$gedc = $head->getGedc();
$version = $gedc->getVersion(); // Returns "7.0" or "5.5.1"

use Gedcom\Writer;

// Write as GEDCOM 5.5.1 (default)
$output551 = Writer::convert($gedcom, Writer::GEDCOM55);
file_put_contents('output_551.ged', $output551);

// Write as GEDCOM 7.0
$output70 = Writer::convert($gedcom, Writer::GEDCOM70);
file_put_contents('output_70.ged', $output70);

$parser = new \Gedcom\Parser();
$gedcom = $parser->parse('tmp.ged');

foreach ($gedcom->getIndi() as $individual) {
    $names = $individual->getName();
    if (!empty($names)) {
        $name = reset($names); // Get the first name object from the array
        echo $individual->getId() . ': ' . $name->getSurn() . ', ' . $name->getGivn() . PHP_EOL;
    }
}
bash
# Basic benchmark
php examples/cli/performance-benchmark.php sample.ged

# Full benchmark with streaming and report
php examples/cli/performance-benchmark.php large.ged --streaming --report

# Save baseline for comparison
php examples/cli/performance-benchmark.php test.ged --baseline

# Compare with baseline
php examples/cli/performance-benchmark.php test.ged --compare