Documentation

Text
in package

Various text helpers such as limiting.

Table of Contents

Methods

alternate()  : string
Alternates between two or more strings.
autoLink()  : string
Converts text email addresses and anchors into links.
autoLinkEmails()  : string
Converts text email addresses into links.
autoLinkUrls()  : string
Converts text anchors into links.
autoP()  : string
Automatically applies <p> and <br /> markup to text. Basically nl2br() on steroids.
bytes()  : string
Returns human readable sizes.
camel2lc()  : string
Convert a CamelCaps or camelCase name into a lower_case names
censor()  : string
Replaces the given words with a string.
containsFormTag()  : bool
Determines whether given HTML contains a FORM tag, which can cause nested-forms issues
copyright()  : string
Returns current year or original year and current year of copyright
lc2camelcaps()  : string
Convert a lower_case names into CamelCaps names
lc2camelcase()  : string
Convert a lower_case names into camelCase names
limitChars()  : string
Limits a plain-text phrase to a given number of characters.
limitedSubsetHtml()  : string
Encode HTML so it's suitable for direct output, but allow some HTML tags to be left as-is
limitWords()  : string
Limits a plain-text phrase to a given number of words.
limitWordsHtml()  : mixed
Limits HTML to a certain number of words.
ordinalize()  : string
Returns a number with an english suffix appended (e.g. 1st, 5th, 12th, 123rd)
plain()  : string
Make a chunk of valid HTML into plain text, and (optionally) limit the number of words.
reduceSlashes()  : string
Reduces multiple slashes in a string to single slashes.
richtext()  : string
Make a chunk of plain text into HTML rich text The text will be wrapped within a block element (default is a P tag)
similar()  : string
Finds the text that is similar between a set of words.
widont()  : string
Prevents widow words by inserting a non-breaking space between the last two words.

Methods

alternate()

Alternates between two or more strings.

public static alternate(string ...$args) : string
Parameters
$args : string

strings to alternate between

Return values
string

Converts text email addresses and anchors into links.

public static autoLink(string $text) : string
Parameters
$text : string

to auto link

Return values
string

autoLinkEmails()

Converts text email addresses into links.

public static autoLinkEmails(string $text) : string
Parameters
$text : string

to auto link

Return values
string

autoLinkUrls()

Converts text anchors into links.

public static autoLinkUrls(string $text) : string
Parameters
$text : string

to auto link

Return values
string

autoP()

Automatically applies <p> and <br /> markup to text. Basically nl2br() on steroids.

public static autoP(string $str) : string
Parameters
$str : string

subject

Return values
string

bytes()

Returns human readable sizes.

public static bytes(int $bytes[, string $force_unit = '' ][, string $format = null ][, bool $si = true ]) : string
Parameters
$bytes : int

size in bytes

$force_unit : string = ''

a definitive unit

$format : string = null

the return string format

$si : bool = true

whether to use SI prefixes or IEC

Tags
see
Based

on original functions written by:

Aidan

Lister: http://aidanlister.com/repos/v/function.size_readable.php

Quentin

Zervaas: http://www.phpriot.com/d/code/strings/filesize-format/

Return values
string

camel2lc()

Convert a CamelCaps or camelCase name into a lower_case names

public static camel2lc(string $name) : string
Parameters
$name : string
Return values
string

censor()

Replaces the given words with a string.

public static censor(string $str, array<string|int, mixed> $badwords[, string $replacement = '#' ][, bool $replace_partial_words = false ]) : string
Parameters
$str : string

Phrase to replace words in

$badwords : array<string|int, mixed>

Words to replace

$replacement : string = '#'

Replacement string

$replace_partial_words : bool = false

Replace words across word boundaries (space, period, etc). This probably doesn't do what you think it does; check the test suite.

Return values
string

containsFormTag()

Determines whether given HTML contains a FORM tag, which can cause nested-forms issues

public static containsFormTag(string $html) : bool

Not tested with malformed input - should not be used as an XSS filter

Parameters
$html : string

HTML to check

Return values
bool —

True if the string contains a FORM tag, false if it doesn't

Returns current year or original year and current year of copyright

public static copyright(string $year) : string
Parameters
$year : string

The original year of copyright

Return values
string —

Current year, or Original year - Current year

lc2camelcaps()

Convert a lower_case names into CamelCaps names

public static lc2camelcaps(string $name) : string
Parameters
$name : string
Return values
string

lc2camelcase()

Convert a lower_case names into camelCase names

public static lc2camelcase(string $name) : string
Parameters
$name : string
Return values
string

limitChars()

Limits a plain-text phrase to a given number of characters.

public static limitChars(string $str[, int $limit = 100 ][, string $end_char = null ][, bool $preserve_words = false ]) : string
Parameters
$str : string

Phrase to limit characters of, in plain text

$limit : int = 100

Number of characters to limit to

$end_char : string = null

Characters to append if text is limited, e.g. '...'

$preserve_words : bool = false

True if whole words should be preserved; false to allow ending on a partial word

Return values
string —

Plain text

limitedSubsetHtml()

Encode HTML so it's suitable for direct output, but allow some HTML tags to be left as-is

public static limitedSubsetHtml(string $html) : string

Only a limited subset of tags are left alone, all other tags are stripped. Allowed tags: A, B, I, STRONG, EM, BR, IMG, SPAN, ABBR, SUP, SUB

The algorithm used in this method is quite simple, so this method should not be used as a defence against XSS attacks; it should only be used on trusted input such as Form helptext.

Parameters
$html : string

Plain text or HTML which may contain various tags

Return values
string —

HTML which only contains safe tags

limitWords()

Limits a plain-text phrase to a given number of words.

public static limitWords(string $str[, int $limit = 100 ][, string $end_char = null ]) : string
Parameters
$str : string

Phrase to limit words of, in plain text

$limit : int = 100

Number of words to limit to

$end_char : string = null

Characters to append if text is limited, e.g. '...'

Return values
string —

Plain text

limitWordsHtml()

Limits HTML to a certain number of words.

public static limitWordsHtml(mixed $text[, mixed $limit = 50 ]) : mixed

Is aware of tags etc and will not count them in the word-count, as well as closing them properly.

This doesn't actually pass all unit tests at the moment - an exact match in num words will still put in ... part.

Parameters
$text : mixed
$limit : mixed = 50

ordinalize()

Returns a number with an english suffix appended (e.g. 1st, 5th, 12th, 123rd)

public static ordinalize(int $number) : string
Parameters
$number : int
Return values
string

plain()

Make a chunk of valid HTML into plain text, and (optionally) limit the number of words.

public static plain(string $html[, int $max_words = 50 ]) : string
Parameters
$html : string

The original HTML

$max_words : int = 50

The maximum number of words. Use 0 for no limit.

Return values
string —

Plain text

reduceSlashes()

Reduces multiple slashes in a string to single slashes.

public static reduceSlashes(string $str) : string
Parameters
$str : string

string to reduce slashes of

Return values
string

richtext()

Make a chunk of plain text into HTML rich text The text will be wrapped within a block element (default is a P tag)

public static richtext(string $text[, string|null $block_elem = 'p' ]) : string
Parameters
$text : string

The original plain text

$block_elem : string|null = 'p'

The block element to use. Default is a P tag (i.e. 'p'). Use null or empty string to get the result without it being wrapped in a tag.

Return values
string —

A HTML representation of the plain text

similar()

Finds the text that is similar between a set of words.

public static similar(array<string|int, mixed> $words) : string
Parameters
$words : array<string|int, mixed>

to find similar text of

Return values
string
On this page

Search results