kstenschke / php-ext-str

PHP extension with assorted string utilities.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ext/str

Set of utility functions for strings in PHP.

Started in 2011 by Martin Jansen, primarily for personal entertainment.
Extended in 2016 by Kay Stenschke.

Most of that stuff isn’t tested that well (though there are unit tests available)
and the API and the behaviour of the functions is likely to change. You have been warned.

Functions

str_contains(haystack, needle [, case_sensitive = false]) : bool

Returns true if the string haystack contains the substring needle. Returns false otherwise.
If the optional third parameter is set to true, the test is performed in a case-insensitive manner.

str_endswith(haystack, needle [, case_sensitive = false]) : bool

Returns true if the string haystack ends with the substring needle. Returns false otherwise.
If the optional third parameter is set to true, the test is performed in a case-insensitive manner.

str_intexplode(haystack [, delimiter = ‘,’]) : int[]

Splits the given string by the given delimiter (or default: “,”) into integers.

str_islower(input) : bool

Returns true if all cased characters in the given input string are lowercase and if the string contains at least one cased character. Returns false otherwise.

str_isupper(input) : bool

Returns true if all cased characters in the given input string are uppercase and if the string contains at least one cased character. Returns false otherwise.

str_iswhitespace(input) : bool

Returns true if the given string is empty or contains whitespace characters only. Returns false otherwise.

str_random(int length [, bool upper = true, bool number = true, bool special = true]) : string

Generates a (mostly) speakable random string of given length and options.

str_startsnumerical(haystack [, allow_negative = false]) : bool

Returns true if the given string starts with a number, or optionally a negative number. Returns false otherwise.

str_startswith(haystack, needle [, case_sensitive = false]) : bool

Returns true if the string haystack starts with the substring needle. Returns false otherwise.
If the optional third parameter is set to true, the test is performed in a case-insensitive manner.

str_swapcase(input) : string

Returns the given string with uppercase characters converted to their lowercase representations and vice versa.

str_unwrap(input, lhs, rhs) : string

Unwraps the given string (removes given left- and right-hand-side strings, if found)

str_wrap(lhs, input, rhs [, prevent_rewrap = false]) : string

Returns the given string wrapped with given left-hand-side and right-hand-side strings.
Optionally checks whether the given string already starts and ends with the given left- and right-hand-side wraps, returns the unaltered input string than.

License

All the stuff in here is licensed under the New BSD License. See the
included LICENSE file for more info. Have fun!

Development notes

Building

$ phpize
$ ./configure
$ make -B

Should you run into issues regarding PHP’s buildconf script at some point,
try forcing autoconf and autoheader to version 2.13. (Make sure those are
installed, of course.)

$ PHP_AUTOHEADER=autoheader213 PHP_AUTOCONF=autoconf213 ./buildconf

Not sure why those things still have to be so complicated in the 21st
century.

Running the tests

$ export TEST_PHP_EXECUTABLE=./sapi/cli/php
$ php run-tests.php ./ext/str/tests/

TODO / ideas

  • add options to str_intexplode – unique, positive
  • str_reducecharrepetitions(string, char, max_repetitions) : string
  • str_removenonnumericchars($str, $convertToInt = false) – Reduce given string to its contained numbers
  • str_filter($str, $allowedChars) – Remove all characters not contained in $allowedChars from $str
  • str_formatamountdigits($str, $digits) – format numeric string to given amount of digits
  • str_camelize($string, $delimiter = ’ ’, $upperCaseFirstLetter = false)
  • str_cameltowords($string, $glue = ’ ’, $upperCaseFirstLetter = true)
  • str_endsnumerical(haystack [, allow_negative = false]) : bool
  • str_between(string, lhs, rhs, offsetLhs = 0, offsetRhs = 0) – get substring after/before/inbetween lhs/rhs
  • str_htmltoplaintext($str) – remove all html tags
  • str_replaceBetween($str, $needle1, $needle2, $replacement)
  • arr_strtolower – perform strtolower() on all items of given array

About

PHP extension with assorted string utilities.

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:C 73.6%Language:PHP 20.1%Language:M4 5.8%Language:JavaScript 0.5%