Text - Simple 1 Class Text Manipulation Library

Related tags

Strings Text
Overview

Text - Simple 1 Class Text Manipulation Library

Latest Stable Version Build Status Coverage Status

Do you remember PHP's string functions? If not, just wrap you text with Text! It will save a minute on your coding.

Text is extracted from kzykhys/Ciconia. this is used for markdown processing.

Installation

Modify your composer.json and run php composer.phar update

{
    "require": {
        "kzykhys/text":"~1.0.0"
    }
}

Requirements

PHP5.4+

Get Started

Text acts like a string

<?php

use KzykHys\Text\Text;

$text = new Text('Lorem Ipsum');
echo $text;

// Lorem Ipsum

Text can also be called statically

<?php

use KzykHys\Text\Text;

$text = Text::create('Lorem Ipsum');
echo $text;

// Lorem Ipsum

Manipulation methods are chainable:

$text = new Text('foo');
$text
    ->append('bar')     // foobar
    ->prepend('baz')    // bazfoobar
    ->wrap('-')         // -bazfoobar-
    ->upper()           // -BAZFOOBAR-
    ->lower()           // -bazfoobar-
    ->trim('-')         // bazfoobar
    ->rtrim('r')        // bazfooba
    ->ltrim('b')        // azfooba
;

Special note for replace()

$text = new Text('FooBarBaz');
$text->replace('/Foo(Bar)(Baz)/', function (Text $whole, Text $bar, Text $baz) {
    return $bar->upper()->append($baz->lower());
});
echo $text;

// BARbaz

If the second argument is callable, callback takes at least one parameter. The whole match being first, and matched subpatterns. All parameters are Text instance.

API

Manipulation (Chainable)

Method Description
create($text) Create a new Text instance.
append($text) Append the string.
prepend($text) Prepend the string.
wrap($start, [$end]) Surround text with given string.
lower() Make a string lowercase.
upper() Make a string uppercase.
trim([$charList]) Strip whitespace (or other characters) from the beginning and end of a string.
rtrim([$charList]) Strip whitespace (or other characters) from the end of a string.
ltrim([$charList]) Strip whitespace (or other characters) from the beginning of a string.
escapeHtml([$option]) Convert special characters to HTML entities.
replace($pattern, $replacement) Perform a regular expression search and replace. If $replacement is the callable, a callback that will be called and passed an array of matched elements in the subject string.
replaceString($search, $replace) Replace all occurrences of the search string with the replacement string.
indent([$spaces]) Add one level of line-leading spaces.
outdent([$spaces]) Remove one level of line-leading tabs or spaces.
detab([$spaces]) Convert tabs to spaces.
eachLine($callback) Apply a user function to every line of the string.

Test

Method Description
isEmpty() Determine whether a variable is empty
isNumeric() Finds whether a variable is a number or a numeric string
match($pattern, [&$matches]) Perform a regular expression match

Miscellaneous

Method Description
split($pattern, [$flags]) Split string by a regular expression
lines([$pattern]) Split string by a line break
chars() Convert a string to an array
length() Gets the length of a string
countLines() Gets the number of lines
indexOf($needle, [$offset]) Find the position of the first occurrence of a substring in a string

Filesystem

Method Description
save($path) Write a string to a file

License

The MIT License

Author

Kazuyuki Hayashi (@kzykhys)

You might also like...
Render Persian Text (UTF-8 Hexadecimals)
Render Persian Text (UTF-8 Hexadecimals)

Persian-Glyphs Purpose This class takes Persian text (encoded in Windows-1256 character set) as input and performs Persian glyph joining on it and out

A PHP library for generating universally unique identifiers (UUIDs).

ramsey/uuid A PHP library for generating and working with UUIDs. ramsey/uuid is a PHP library for generating and working with universally unique ident

The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.

DeviceDetector Code Status Description The Universal Device Detection library that parses User Agents and detects devices (desktop, tablet, mobile, tv

A fast PHP slug generator and transliteration library that converts non-ascii characters for use in URLs.

URLify for PHP A fast PHP slug generator and transliteration library, started as a PHP port of URLify.js from the Django project. Handles symbols from

🉑 Portable UTF-8 library - performance optimized (unicode) string functions for php.

🉑 Portable UTF-8 Description It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your

ColorJizz is a PHP library for manipulating and converting colors.

#Getting started: ColorJizz-PHP uses the PSR-0 standards for namespaces, so there should be no trouble using with frameworks like Symfony 2. ###Autolo

🔡 Portable ASCII library - performance optimized (ascii) string functions for php.

🔡 Portable ASCII Description It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your

Library for free use Google Translator. With attempts connecting on failure and array support.

GoogleTranslateForFree Packagist: https://packagist.org/packages/dejurin/php-google-translate-for-free Library for free use Google Translator. With at

PHP library to parse urls from string input

Url highlight - PHP library to parse URLs from string input. Works with complex URLs, edge cases and encoded input. Features: Replace URLs in string b

Comments
  • Text methods return

    Text methods return

    Hi,

    As I saw in your Markdown library, and so forth here, each method that manipulates a string through your Text object are modifiying the content of the original method (methods like trim, replace, ...)

    I think that, as for example Javascript does, you shouldn't modify directly the text, but instead return a new instance. I think it would make more sense in an object logic.

    I could be wrong though ; i'd be happy to do a PR if that is not the case. :)

    Cheers.

    opened by Taluu 3
  • adding support for \Countable, \ArrayAccess and \Iterator interfaces

    adding support for \Countable, \ArrayAccess and \Iterator interfaces

    This commit adds support for 3 more Intefaces, now is possible this kind of code:

    $t = new Text('hello this is my test');
    
    echo count($t);
    
    echo $t[2];
    
    foreach($t as $c) {
        echo $c;
        }
    
    opened by xr09 2
Owner
Kazuyuki Hayashi
Kazuyuki Hayashi
:accept: Stringy - A PHP string manipulation library with multibyte support, performance optimized

?? Stringy A PHP string manipulation library with multibyte support. Compatible with PHP 7+ 100% compatible with the original "Stringy" library, but t

Lars Moelleken 144 Dec 12, 2022
A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM.

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM. s('string')->toTitleCase()->ensureRight('y') ==

Daniel St. Jules 2.5k Dec 28, 2022
A language detection library for PHP. Detects the language from a given text string.

language-detection Build Status Code Coverage Version Total Downloads Minimum PHP Version License This library can detect the language of a given text

Patrick Schur 738 Dec 28, 2022
Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Motto: "Every business should have a detection script to detect mobile readers." About Mobile Detect is a lightweight PHP class for detecting mobile d

Şerban Ghiţă 10.2k Jan 4, 2023
A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.

SqlFormatter A lightweight php class for formatting sql statements. It can automatically indent and add line breaks in addition to syntax highlighting

Jeremy Dorn 3.9k Jan 1, 2023
A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.

SqlFormatter A lightweight php class for formatting sql statements. It can automatically indent and add line breaks in addition to syntax highlighting

Jeremy Dorn 3.9k Jan 3, 2023
A tiny PHP class-based program to analyze an input file and extract all of that words and detect how many times every word is repeated

A tiny PHP class-based program to analyze an input file and extract all of that words and detect how many times every word is repeated

Max Base 4 Feb 22, 2022
A PHP class which allows the decoding and encoding of a wider variety of characters compared to the standard htmlentities and html_entity_decode functions.

The ability to encode and decode a certain set of characters called 'Html Entities' has existed since PHP4. Amongst the vast number of functions built into PHP, there are 4 nearly identical functions that are used to encode and decode html entities; despite their similarities, however, 2 of them do provide additional capabilities not available to the others.

Gavin G Gordon (Markowski) 2 Nov 12, 2022
"結巴"中文分詞:做最好的 PHP 中文分詞、中文斷詞組件。 / "Jieba" (Chinese for "to stutter") Chinese text segmentation: built to be the best PHP Chinese word segmentation module.

jieba-php "結巴"中文分詞:做最好的 PHP 中文分詞、中文斷詞組件,目前翻譯版本為 jieba-0.33 版本,未來再慢慢往上升級,效能也需要再改善,請有興趣的開發者一起加入開發!若想使用 Python 版本請前往 fxsjy/jieba 現在已經可以支援繁體中文!只要將字典切換為 bi

Fukuball Lin 1.2k Dec 31, 2022
Paranoid text spacing in PHP

pangu.php Paranoid text spacing for good readability, to automatically insert whitespace between CJK (Chinese, Japanese, Korean) and half-width charac

Clancey Lin 79 May 29, 2022