Download the PHP package masakielastic/striter without Composer
On this page you can find all versions of the php package masakielastic/striter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download masakielastic/striter
More information about masakielastic/striter
Files in masakielastic/striter
Package striter
Short Description PHP extension for string iteration with grapheme, codepoint, and byte modes
License
Informations about the package striter
PHP String Iterator Extension
A PHP extension that provides advanced string iteration capabilities for UTF-8 strings with support for grapheme clusters, Unicode codepoints, and byte-level iteration.
Features
- Grapheme Cluster Iteration: Iterate over grapheme clusters (user-perceived characters) using PCRE2
- Unicode Codepoint Iteration: Iterate over individual Unicode codepoints
- Byte-level Iteration: Iterate over individual bytes for low-level string processing
- UTF-8 Safe: Proper handling of multibyte UTF-8 characters
- Standard PHP Interfaces: Implements Iterator, IteratorAggregate, and Countable interfaces for seamless integration
Installation
Requirements
- PHP 8.1 or higher
- PCRE2 library (libpcre2-dev)
Using PIE (Recommended)
PIE (PHP Installer for Extensions) is the recommended way to install this extension.
PIE automatically handles building and enabling the extension.
Build from Source
Enable Extension
Add to your php.ini:
Usage
Basic Usage
Iteration Modes
Grapheme Mode (Default)
Iterates over grapheme clusters (user-perceived characters):
Codepoint Mode
Iterates over individual Unicode codepoints:
Byte Mode
Iterates over individual bytes:
Using Countable Interface
Using IteratorAggregate Interface
API Reference
Functions
str_iter(string $str, string $mode = "grapheme")
Creates a new string iterator.
Parameters:
$str(string): The string to iterate over$mode(string, optional): Iteration mode - "grapheme", "codepoint", or "byte"
Returns: _StrIterIterator object
Iterator Methods
The returned iterator implements PHP's IteratorAggregate and Countable interfaces:
IteratorAggregate Methods:
getIterator(): Returns the iterator itself for nested iteration
Countable Methods:
count(): Returns the total number of elements in the iterator
Examples
Working with Emoji and Complex Characters
Processing Japanese Text
Binary Data Processing
Technical Details
Grapheme Cluster Detection
The extension uses PCRE2's \X pattern to detect grapheme clusters, which properly handles:
- Base characters with combining marks
- Emoji sequences
- Regional indicator sequences
- Hangul syllable sequences
UTF-8 Validation
The extension includes proper UTF-8 validation and handles invalid sequences gracefully by treating them as individual bytes.
Memory Management
The extension properly manages memory for string copies and PCRE2 objects, preventing memory leaks.
Testing
Run the included test files:
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
This project is open source. Please refer to the project's license file for details.
Changelog
Version 0.1.0
- Initial release
- Support for grapheme, codepoint, and byte iteration modes
- PCRE2 integration for proper grapheme cluster detection
- Full Iterator interface implementation