Text Extraction
The document text extraction methods allow you to retrieve various information from the source template. For example, retrieving entire words at specific character offsets or their neighboring words is trivial.
Text Extraction and Node Content Changes
The text extraction methods interact with the original content analyzed by the parsers and related systems. Suppose you have changed a node's content or other properties. In that case, you will need to create a new Document instance by calling the toDocument method for those changes to be available to the text extraction methods.
#Available Methods
#extractText
Extracts the text from the document.
This method will extract the literal, non-Blade text from the document. By default, this method will reverse Blade escape sequences in the produced text. This behavior can be changed by supplying a "falsey" value for the $unEscaped
parameter.
Argument | Description |
---|---|
$unEscaped | Whether to return unescaped text. |
1<?php
2
3public function extractText(
4 bool $unEscaped = true
5): string;
Example Use
1<?php
2
3use Stillat\BladeParser\Document\Document;
4
5$template = <<<'BLADE'
6@@if ($this) {{ $hello }} @@endif
7BLADE;
8
9$doc = Document::fromText($template);
10// Returns "@if ($this) @endif"
11$unEscaped = $doc->extractText();
12
13// Returns "@@if ($this) @@endif"
14$escaped = $doc->extractText(false);
#getText
Returns the original document text between two character offsets.
Argument | Description |
---|---|
$startOffset | The start offset. |
$endOffset | The end offset. |
1<?php
2
3public function getText(int $startOffset,
4 int $endOffset): string;
Example Use
1<?php
2
3use Stillat\BladeParser\Document\Document;
4
5$template = <<<'BLADE'
6 Hello, {{ $world }}!
7BLADE;
8
9$doc = Document::fromText($template);
10
11// Returns "Hello,"
12$text = $doc->getText(4, 10);
#getLineCount
Returns the number of lines in the document.
1<?php
2
3public function getLineCount(): int;
#getWordAtOffset
Retrieves a word from the source document at the provided character offset.
Argument | Description |
---|---|
$offset | The character offset. |
$chars | A list of characters that won't break a word. |
1<?php
2
3public function getWordAtOffset(int $offset,
4 array $chars = ['-']): string;
Example Use
1<?php
2
3use Stillat\BladeParser\Document\Document;
4
5$doc = Document::fromText('one two three');
6
7// Returns "two"
8$word = $doc->getWordAtOffset(5);
#getWordLeftAtOffset
Retrieves the word to the left of the provided offset, ignoring the word at the provided offset.
Argument | Description |
---|---|
$offset | The character offset. |
$chars | A list of characters that won't break a word. |
1<?php
2
3public function getWordLeftAtOffset(int $offset,
4 array $chars = ['-']): string;
Example Use
1<?php
2
3use Stillat\BladeParser\Document\Document;
4
5$doc = Document::fromText('one two three');
6
7// Returns "one"
8$word = $doc->getWordLeftAtOffset(5);
#getWordRightAtOffset
Retrieves the word to the right of the provided offset, ignoring the word at the provided offset.
Argument | Description |
---|---|
$offset | The character offset. |
$chars | A list of characters that won't break a word. |
1<?php
2
3public function getWordRightAtOffset(int $offset,
4 array $chars = ['-']): string;
Example Use
1<?php
2
3use Stillat\BladeParser\Document\Document;
4
5$doc = Document::fromText('one two three');
6
7// Returns "three"
8$word = $doc->getWordRightAtOffset(5);
#getLineExcerpt
Retrieves the requested line number, and a specified number of lines surrounding it.
The keys of the returned array will correspond to the line number the text was extracted from.
Argument | Description |
---|---|
$lineNumber | The target line number. |
$radius | The number of desired lines surrounding the target line number. |
1<?php
2
3public function getLineExcerpt(int $lineNumber,
4 int $radius = 2): array;
Example Use
1<?php
2
3use Stillat\BladeParser\Document\Document;
4
5$template = <<<'BLADE'
6Line 1
7Line 2
8Line 3
9Line 4
10Line 5
11Line 6
12Line 7
13Line 8
14BLADE;
15
16$doc = Document::fromText($template);
17
18// Returns an array containing the following strings:
19// 2 => "Line 2"
20// 3 => "Line 3"
21// 4 => "Line 4"
22// 5 => "Line 5"
23// 6 => "Line 6"
24$lines = $doc->getLineExcerpt(4, 2);
#getLines
Retrieves a string array, containing the lines of the source document.
1<?php
2
3public function getLines(): array;