一个 PHP HTML 到纯文本的转换器 Transformer。
Hypertext
A PHP HTML to pure text transformer that beautifully handles various and malformed HTML.
Hypertext is excellent at pulling text content out of any HTML based document and automatically:
It is directed at using the output in LLM related tasks, such as prompts and embeddings.
composer require stevebauman/hypertextuse Stevebauman\Hypertext\Transformer;
$transformer = new Transformer();
// (Optional) Filter out specific elements by their XPath.
$transformer->filter("//*[@id='some-element']");
// (Optional) Retain new line characters.
$transformer->keepNewLines();
// (Optional) Retain anchor tags and their href attribute.
$transformer->keepLinks();
$text = $transformer->toText($html);For larger examples, please view the tests/Fixtures directory.
Input:
My Blog
Welcome to My Blog
This is a paragraph of text on my webpage.
Click here to view my posts.
Output (Pure Text):
echo (new Transformer)->toText($html);Welcome to My Blog This is a paragraph of text on my webpage. Click here to view my posts.Output (Keep New Lines):
echo (new Transformer)->keepNewLines()->toText($html);Welcome to My Blog
This is a paragraph of text on my webpage.
Click here to view my posts.Output (Keep Links):
echo (new Transformer)->keepLinks()->toText($html);Welcome to My Blog This is a paragraph of text on my webpage. Click Here to view my posts.Output (Keep Both):
echo (new Transformer)
->keepLinks()
->keepNewLines()
->toText($html);Welcome to My Blog
This is a paragraph of text on my webpage.
Click Here to view my posts.暂无开放 Issues,或尚未同步最近议题。