PHP code example of stolt / llms-txt-php

1. Go to this page and download the library: Download stolt/llms-txt-php 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/ */

    

stolt / llms-txt-php example snippets




use Stolt\LlmsTxt\LlmsTxt;
use Stolt\LlmsTxt\Section;
use Stolt\LlmsTxt\Section\Link;

$llmsTxt = (new LlmsTxt())
    ->title('My project')
    ->description('A short description of my project.')
    ->details('Additional information about the project.')
    ->addSection(
        (new Section())
            ->name('Documentation')
            ->addLink(
                (new Link())
                    ->urlTitle('Getting started')
                    ->url('https://example.com/docs')
                    ->urlDetails('Getting started guide')
            )
    );

echo $llmsTxt->toString();

$llmsTxt->toFile('/path/to/llms.txt');

use Stolt\LlmsTxt\LlmsTxt;

$llmsTxt = (new LlmsTxt())->parse('/path/to/llms.txt');

$title = $llmsTxt->getTitle();
$description = $llmsTxt->getDescription();
$details = $llmsTxt->getDetails();
$sections = $llmsTxt->getSections();

use Stolt\LlmsTxt\Parser\LlmsTxtParser;

$parser = new LlmsTxtParser();

$llmsTxt = $parser->parse($markdown);
$llmsTxt = $parser->parseFile('/path/to/llms.txt');

if ($llmsTxt->validate()) {
    // The document is valid.
}

$validationResult = $llmsTxt->validate(true);

if (!$validationResult->isValid()) {
    foreach ($validationResult->errors() as $error) {
        // Handle validation error.
    }
}

if ($validationResult->hasWarnings()) {
    foreach ($validationResult->warnings() as $warning) {
        // Handle validation warning.
    }
}

$llmsTxt->optional(
    (new Section())->addLink(
        (new Link())
            ->urlTitle('Secondary link')
            ->url('https://example.com/secondary')
    )
);

$optionalSection = $llmsTxt->getOptional(); // null when absent

use Stolt\LlmsTxt\Discovery;

$discovery = new Discovery();

$elements = $discovery->asLinkElements(
    '/docs/llms.txt',
    '/docs/page.html.md'
);

$header = $discovery->asLinkHeader(
    '/docs/llms.txt',
    '/docs/page.html.md'
);

header('Link: ' . $header);

$discovery->describedByUrls($html);
// ['/docs/llms.txt']

$discovery->markdownAlternates($html);
// ['/docs/page.html.md']

$discovery->describedByUrlsFromHeader($header);
$discovery->markdownAlternatesFromHeader($header);

$discovery->markdownUrls('/docs/page.html');
// ['/docs/page.html.md', '/docs/page.md']

$discovery->markdownUrls('/docs/');
// ['/docs/index.html.md', '/docs/index.md']

$discovery->coveringUrls('/docs/a/b.html');
// ['/docs/a/llms.txt', '/docs/llms.txt', '/llms.txt']

$discovery->coveringUrl(
    '/docs/a/b.html',
    ['/llms.txt', '/docs/llms.txt']
);
// '/docs/llms.txt'

use Stolt\LlmsTxt\Discovery;
use Stolt\LlmsTxt\LlmsTxt;

$discovery = new Discovery();

foreach ($discovery->describedByUrls($html) as $llmsTxtUrl) {
    $llmsTxt = (new LlmsTxt())->parse($llmsTxtUrl);

    if ($llmsTxt->validate()) {
        $sections = $llmsTxt->getSections();

        // Use the discovered context.
    }
}

$llmsTxts = [
    '/llms.txt' => (new LlmsTxt())->parse('/path/to/llms.txt'),
    '/docs/api/llms.txt' => (new LlmsTxt())->parse('/path/to/docs/api/llms.txt'),
];

$page = '/docs/api/v2/endpoints.html';

$coveringUrl = $discovery->coveringUrl(
    $page,
    array_keys($llmsTxts)
);

header('Link: ' . $discovery->asLinkHeader(
    $coveringUrl ?? '',
    $discovery->markdownUrls($page)[0]
));

use Stolt\LlmsTxt\LlmsTxt;

$llmsTxt = (new LlmsTxt())->parse('/path/to/llms.txt');

$context = $llmsTxt->toLlmContext();

$llmsTxt->toLlmContextFile('/path/to/llm-ctx.xml');

$context = $llmsTxt->toLlmContext(true);

$fetcher = static function (string $url): string {
    // Fetch and return the document contents.
};

$context = $llmsTxt->toLlmContext(false, $fetcher);

$full = $llmsTxt->toFull();

$llmsTxt->toFullFile('/path/to/llms-full.txt');

$full = $llmsTxt->toFull(true);

$embedded = $llmsTxt->asScriptTag();

use Stolt\LlmsTxt\Extractor;

$html = <<<HTML
<html>
    <body>
        <script type="text/llms.txt"># First document</script>
        <p>Regular page content.</p>
        <script type="text/llms.txt"># Second document</script>
    </body>
</html>
HTML;

$llmsTxts = (new Extractor())->extractFromHtml($html);

[
    '# First document',
    '# Second document',
]

$llmsTxts = (new Extractor())->extractFromFile('/path/to/page.html');

$llmsTxts = (new Extractor())->extractFromHtml($html, true);

foreach ($llmsTxts as $llmsTxt) {
    echo $llmsTxt->getTitle();
}

$section = (new Section())
    ->name('Documentation')
    ->addLink($link);

$link = (new Link())
    ->urlTitle('Getting started')
    ->url('https://example.com/docs')
    ->urlDetails('Getting started guide');
bash
composer 
bash
composer