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

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[]
# 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

Throws

Exception
if the AttributedString cannot be converted to HTML due to improper nesting
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
# 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()

Properties summary

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