PHP code example of xianzhe18 / bibtex-handler

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

    

xianzhe18 / bibtex-handler example snippets


use Xianzhe18\BibTexParser\Listener;
use Xianzhe18\BibTexParser\Parser;

y: The Special and General Theory},
  author={Einstein, Albert},
  year={1916}
}
BIBTEX;

$parser = new Parser();          // Create a Parser
$listener = new Listener();      // Create and configure a Listener
$parser->addListener($listener); // Attach the Listener to the Parser
$parser->parseString($bibtex);   // or parseFile('/path/to/file.bib')
$entries = $listener->export();  // Get processed data from the Listener

print_r($entries);

use Xianzhe18\BibTexParser\Processor\TagNameCaseProcessor;

$listener->addProcessor(new TagNameCaseProcessor(CASE_UPPER)); // or CASE_LOWER

use Xianzhe18\BibTexParser\Processor\NamesProcessor;

$listener->addProcessor(new NamesProcessor());

use Xianzhe18\BibTexParser\Processor\KeywordsProcessor;

$listener->addProcessor(new KeywordsProcessor());

use Xianzhe18\BibTexParser\Processor\LatexToUnicodeProcessor;

$listener->addProcessor(new LatexToUnicodeProcessor());

$listener->addProcessor(function (array $entry) {
    $entry['title'] .= ' with laser';
    return $entry;
});

$parser = new Parser();          // Create a Parser
$listener = new Listener();      // Create and configure a Listener

$listener->addProcessor(function (array $entry) { // Custom processor
    $entry['some-key'] = 'some-value';
    return $entry;
});

$parser->addListener($listener); // Attach the Listener to the Parser
$parser->parseString($bibtex);   // or parseFile('/path/to/file.bib')
$entries = $listener->export();  // Get processed data from the Listener

$newBibtex = $parser->convertIntoBibTex($entries);

echo $newBibtex;

$parser = new Parser();          // Create a Parser

$bibtex = $parser->convertIntoBibTex($entries);

echo $newBibtex;

use Xianzhe18\BibTexParser\Exception\ExceptionInterface;
use Xianzhe18\BibTexParser\Exception\ParserException;
use Xianzhe18\BibTexParser\Exception\ProcessorException;

try {
    // ... parser and listener configuration

    $parser->parseFile('/path/to/file.bib');
    $entries = $listener->export();
} catch (ParserException $exception) {
    // The BibTeX isn't valid
} catch (ProcessorException $exception) {
    // Listener's processors aren't able to handle data found
} catch (ExceptionInterface $exception) {
    // Alternatively, you can use this exception to catch all of them at once
}

interface Xianzhe18\BibTexParser\ListenerInterface
{
    /**
     * Called when an unit is found.
     *
     * @param string $text    The original content of the unit found.
     *                        Escape character will not be sent.
     * @param string $type    The type of unit found.
     *                        It can assume one of Parser's constant value.
     * @param array  $context Contains details of the unit found.
     */
    public function bibTexUnitFound($text, $type, array $context);
}