https://github.com/donatj/printf-parser
A PHP compatible tokenizing printf string parser
https://github.com/donatj/printf-parser
parser php php-library printf
Last synced: 9 months ago
JSON representation
A PHP compatible tokenizing printf string parser
- Host: GitHub
- URL: https://github.com/donatj/printf-parser
- Owner: donatj
- License: mit
- Created: 2019-03-16T00:46:36.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-03-03T03:31:58.000Z (11 months ago)
- Last Synced: 2025-03-29T23:11:47.546Z (10 months ago)
- Topics: parser, php, php-library, printf
- Language: PHP
- Homepage:
- Size: 106 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Printf Parser
[](https://packagist.org/packages/donatj/printf-parser)
[](https://packagist.org/packages/donatj/printf-parser)
[](https://packagist.org/packages/donatj/printf-parser)
[](https://github.com/donatj/printf-parser/actions/workflows/ci.yml)
[](https://scrutinizer-ci.com/g/donatj/printf-parser)
[](https://scrutinizer-ci.com/g/donatj/printf-parser)
PHP printf-syntax compatible printf string parser.
Parses printf strings into a stream of lexemes.
## Requirements
- **php**: >=7.4
## Installing
Install the latest version with:
```bash
composer require 'donatj/printf-parser'
```
## Example
Here is a simple example:
```php
parseStr('percent of %s: %d%%');
$lexemes = $emitter->getLexemes();
foreach( $lexemes as $lexeme ) {
echo $lexeme->getLexItemType() . ' -> ';
echo var_export($lexeme->getVal(), true);
if( $lexeme instanceof \donatj\Printf\ArgumentLexeme ) {
echo ' arg type: ' . $lexeme->argType();
}
echo PHP_EOL;
}
```
Output:
```
! -> 'percent of '
s -> 's' arg type: string
! -> ': '
d -> 'd' arg type: int
! -> '%'
```
## Documentation
### Class: donatj\Printf\Parser
Parser implements a PHP Printf compatible Printf string parser.
#### Method: Parser->__construct
```php
function __construct(\donatj\Printf\Emitter $emitter)
```
Parser constructor.
##### Parameters:
- ***\donatj\Printf\Emitter*** `$emitter` - The given Emitter to emit Lexemes as parsed
---
#### Method: Parser->parseStr
```php
function parseStr(string $string) : void
```
Parses a printf string and emit parsed lexemes to the configured Emitter
### Class: donatj\Printf\LexemeEmitter
---
#### Method: LexemeEmitter->getLexemes
```php
function getLexemes() : \donatj\Printf\LexemeCollection
```
Return the Lexemes received by the emitter as an immutable LexemeCollection
### Class: donatj\Printf\LexemeCollection
LexemeCollection is an immutable iterable collection of Lexemes with ArrayAccess
---
#### Method: LexemeCollection->getInvalid
```php
function getInvalid() : ?\donatj\Printf\Lexeme
```
Retrieve the first invalid Lexeme or null if all are valid.
This is useful for checking if a printf string parsed without error.
---
#### Method: LexemeCollection->toArray
```php
function toArray() : array
```
Get the LexemeCollection as an ordered array of Lexemes
##### Returns:
- ***\donatj\Printf\Lexeme[]***
---
#### Method: LexemeCollection->argTypes
```php
function argTypes() : array
```
##### Returns the list of expected arguments a 1-indexed map of the following
```
ArgumentLexeme::ARG_TYPE_MISSING
ArgumentLexeme::ARG_TYPE_INT
ArgumentLexeme::ARG_TYPE_DOUBLE
ArgumentLexeme::ARG_TYPE_STRING
```
##### Returns:
- ***string[]***
### Class: donatj\Printf\Lexeme
Lexeme represents a "basic" component of a printf string - either Literal Strings "!" or Invalid Lexemes
```php
__construct
```php
function __construct(string $lexItemType, string $val, int $pos)
```
LexItem constructor.
---
#### Method: Lexeme->getLexItemType
```php
function getLexItemType() : string
```
The type of the printf Lexeme
---
#### Method: Lexeme->getVal
```php
function getVal() : string
```
The text of the lexeme
---
#### Method: Lexeme->getPos
```php
function getPos() : int
```
The string position of the given lexeme
### Class: donatj\Printf\ArgumentLexeme
```php
__construct
```php
function __construct(string $lexItemType, string $val, int $pos, ?int $arg, bool $showPositive, ?string $padChar, ?int $padWidth, bool $leftJustified, ?int $precision)
```
ArgumentLexeme constructor.
LexItem constructor.
---
#### Method: ArgumentLexeme->getArg
```php
function getArg() : ?int
```
The position specifier, such as `%3$s` would return 3 and `%s` would return null
##### Returns:
- ***int*** | ***null*** - null on unspecified
---
#### Method: ArgumentLexeme->getShowPositive
```php
function getShowPositive() : bool
```
Is the "Prefix positive numbers with a plus sign +" flag enabled
---
#### Method: ArgumentLexeme->getPadChar
```php
function getPadChar() : ?string
```
Specified pad character flag
##### Returns:
- ***string*** | ***null*** - null on unspecified
---
#### Method: ArgumentLexeme->getPadWidth
```php
function getPadWidth() : ?int
```
Specified pad width
##### Returns:
- ***int*** | ***null*** - null on unspecified
---
#### Method: ArgumentLexeme->getLeftJustified
```php
function getLeftJustified() : bool
```
Is left-justification flag enabled?
---
#### Method: ArgumentLexeme->getPrecision
```php
function getPrecision() : ?int
```
The Lexeme's indicated precision.
##### Returns:
- ***int*** | ***null*** - null on unspecified
---
#### Method: ArgumentLexeme->argType
```php
function argType() : string
```
Returns based on the type of argument one of the following
ArgumentLexeme::ARG_TYPE_MISSING
ArgumentLexeme::ARG_TYPE_INT
ArgumentLexeme::ARG_TYPE_DOUBLE
ArgumentLexeme::ARG_TYPE_STRING
---
#### Method: ArgumentLexeme->getLexItemType
```php
function getLexItemType() : string
```
The type of the printf Lexeme
---
#### Method: ArgumentLexeme->getVal
```php
function getVal() : string
```
The text of the lexeme
---
#### Method: ArgumentLexeme->getPos
```php
function getPos() : int
```
The string position of the given lexeme