Overview
  • Namespace
  • Class

Namespaces

  • apemsel
    • AttributedString

Classes

  • AttributedString
  • MutableAttributedString
  • TokenizedAttributedString

Class AttributedString

Basic class to work with attributed strings.

Attributed strings are strings that can have multiple attributes per character of the string

apemsel\AttributedString\AttributedString implements Countable, ArrayAccess

Direct known subclasses

apemsel\AttributedString\MutableAttributedString, apemsel\AttributedString\TokenizedAttributedString

Namespace: apemsel\AttributedString
Author: Adrian Pemsel apemsel@gmail.com
Located at AttributedString.php

Methods summary

public
# __construct( string|apemsel\AttributedString\AttributedString $string )

Parameters

$string
Either a simple string or another AttributedString to init the AttributedString
public string
# __toString( )

Returns the native string

Returns the native string

Returns

string
The native string representation of the AttributedString without attributes
public
# createAttribute( string $attribute )

Creates a new attribute layer

Creates a new attribute layer

Parameters

$attribute
The name of the new attribute

Throws

InvalidArgumentException
if the attribute already exists
public boolean
# hasAttribute( string $attribute )

Check if the given attribute exists

Check if the given attribute exists

Parameters

$attribute
The name of the attribute to check

Returns

boolean
public
# deleteAttribute( $attribute )
public
# setRange( integer $from, integer $to, string $attribute, boolean $state = true )

Set given range of the string to an attribute and state

Set given range of the string to an attribute and state

Parameters

$from
start offset
$to
end offset
$attribute
name of the attribute to be set
$state
set state to true (default) or false
public
# setLength( integer $from, integer $length, string $attribute, boolean $state = true )

Set given length of the string to an attribute and state

Set given length of the string to an attribute and state

Parameters

$from
start offset
$length
length to be set
$attribute
name of the attribute to be set
$state
set state to true (default) or false
public integer
# setPattern( string $pattern, string $attribute, boolean $state = true )

Set parts of the string matching a given regex to an attribute and state

Set parts of the string matching a given regex to an attribute and state

Parameters

$pattern
regex pattern
$attribute
name of the attribute to be set
$state
set state to true (default) or false

Returns

integer
number of matches
public
# setSubstring( string $substring, string $attribute, boolean $all = true, boolean $matchCase = true, boolean $state = true )

Set given substring to an attribute and state

Set given substring to an attribute and state

Parameters

$substring
the substring to search
$attribute
name of the attribute to be set
$all
set first or all occurences of the substring
$matchCase
match or ignore case
$state
set state to true (default) or false
public integer|int[]
# searchAttribute( string $attribute, integer $offset = 0, boolean $returnLength = false, boolean $state = true, boolean $strict = true )

Search inside the string for ranges with the given attribute

Search inside the string for ranges with the given attribute

Parameters

$attribute
name of the attribute to search
$offset
start offset
$returnLength
if true (default is false), return an array with position and length of the found range
$state
the state to look for (default is true)
$strict
perform strict comparison during search

Returns

integer|int[]
either position or position and lenght in an array
public boolean
# is( string $attribute, integer $pos )

Check for given attribute at a offset

Check for given attribute at a offset

Parameters

$attribute
name of the attribute to check
$pos
offset to check

Returns

boolean
true if string has the attribute at the given position
public string[]
# substrings( string $attribute, integer $offset = 0, boolean $state = true, boolean $strict = true )

Return an array of substrings that have a given attribute

Return an array of substrings that have a given attribute

Parameters

$attribute
name of the attribute
$offset
$pos offset
$state
the state to look for (default is true)
$strict
perform strict comparison during search

Returns

string[]
array of strings with given attribute
public string
# filter( string $attribute, integer $offset = 0, boolean $state = true, boolean $strict = true, string $glue = "" )

Return all parts of the string that have a given attribute as new string

Return all parts of the string that have a given attribute as new string

Parameters

$attribute
name of the attribute
$offset
$pos offset
$state
the state to look for (default is true)
$strict
perform strict comparison during search
$glue
glue that is inserted between the parts, default is nothing ("")

Returns

string
combined filtered string
public string[]
# attributesAt( integer $pos )

Return all attributes at a given offset

Return all attributes at a given offset

Parameters

$pos
offset

Returns

string[]
attributes at the given offset
public string
# toHtml( string $tag = "span", string $classPrefix = "" )

Convert to HTML, using a given class to mark attribute spans

Convert to HTML, using a given class to mark attribute spans

Parameters

$tag
HTML tag to use for the spans (defaults is "")
$classPrefix
Optional prefix used to convert the attribute names to class names

Returns

string
HTML
public
# combineAttributes( string $op, string $attribute1, string $attribute2 = false, string $to = false )

Combine attributes with the given boolean operation

Combine attributes with the given boolean operation

Parameters

$op
one of or|xor|and|not
$attribute1
name of the first attribute
$attribute2
Name of the second attribute. Ignored for "not" operation.
$to
optional name of the attribute to copy the result to

Throws

InvalidArgumentException
if one of the attributes does not exist or an unkown operation is given
public
# attributeToString( string $attribute, string $true = "-", string $false = " " )

Convert attribute map to a visual string representation (e.g. for debugging)

Convert attribute map to a visual string representation (e.g. for debugging)

Parameters

$attribute
name of the attribute
$true
char to use for true state of attribute
$false
char to use for false state of attribute
public
# enablebyteToCharCache( )

Enable and fill cache for byte to char offset conversion

Enable and fill cache for byte to char offset conversion

May improve performance if setPattern is used extensively

protected
# byteToCharOffset( $boff )
protected
# charToByteOffset( $char )
protected static
# byteToCharOffsetString( $string, $boff )
protected static
# utf8CharLen( $byte )
public integer
# count( )

Return string length (number of UTF-8 chars, not strlen())

Return string length (number of UTF-8 chars, not strlen())

Returns

integer
string length

Implementation of

Countable::count()
public boolean
# offsetExists( integer $offset )

Check if the given offset exists in the string

Check if the given offset exists in the string

Parameters

$offset
offset

Returns

boolean
does the offset exist

Implementation of

ArrayAccess::offsetExists()
public string
# offsetGet( integer $offset )

Get char at given offset

Get char at given offset

Note: Since AttributedString is using UTF-8, the returned char may be longer than 1 byte!

Parameters

$offset
offset

Returns

string
character

Implementation of

ArrayAccess::offsetGet()
public
# offsetSet( $offset, $value )

Not implemented since AttributedString is immutable

Not implemented since AttributedString is immutable

Throws

InvalidArgumentException
always

Implementation of

ArrayAccess::offsetSet()
public
# offsetUnset( $offset )

Not implemented since AttributedString is immutable

Not implemented since AttributedString is immutable

Throws

InvalidArgumentException
always

Implementation of

ArrayAccess::offsetUnset()

Properties summary

protected $string
#
protected array $attributes
# []
protected $length
#
protected $byteToChar
#
API documentation generated by ApiGen