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'
);
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.
}
}
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);