pclib  3.0.0
Lightweight PHP framework
Static Public Member Functions
Str Class Reference

String utilities for utf-8 strings. More...

Static Public Member Functions

static format ($str, array $params, $keepEmpty=false)
 Replace {param} placeholders in string with values from array $param. More...
 
static extractPath ($path, $format)
 Extract part of the filesystem path. More...
 
static htmlTag ($name, array $attr=[], $content=null)
 Create html tag. More...
 
static random ($size=16, $characters=self::ALPHANUM)
 Return random string with $size characters. More...
 
static match ($str, $pattern)
 Match $pattern on $str and return result or empty string. More...
 
static matchAll ($str, $pattern)
 Match $pattern on $str and return all results as array. More...
 
static replace ($str, $search, $replace)
 Replace all occurences of $search in $str with $replace. More...
 
static substr ($str, $start, $length=null)
 
static strpos ($str, $search, $offset=0)
 
static length ($str)
 
static upper ($str)
 
static lower ($str)
 
static lpad ($str, $length, $pad=' ')
 
static rpad ($str, $length, $pad=' ')
 
static startsWith ($str, $search)
 Return true if $str starts with $search.
 
static endsWith ($str, $search)
 Return true if $str ends with $search.
 
static filter ($str, $preserve)
 Preserve only specified characters in $str. More...
 
static contains ($str, $search, $ignoreCase=false)
 Return true if $str contains $search. More...
 
static webalize ($str)
 Transform string $str into url-ready form 'some-string-1234'. More...
 
static id ($str, $preserve='\w', $separator='_')
 Remove diacriticts and non-alphanum characters from $str and replace spaces with '_'. More...
 
static ascii ($str)
 Replace characters with diacritics to plain ascii characters.
 
static htmlspecialchars ($str)
 

Detailed Description

String utilities for utf-8 strings.

PHP mbstring extension is required. Example of usage: print Str::upper("Hello world");

Member Function Documentation

◆ contains()

static contains (   $str,
  $search,
  $ignoreCase = false 
)
static

Return true if $str contains $search.

Parameters
string$str
string$search
bool$ignoreCase
Returns
bool $result

◆ extractPath()

static extractPath (   $path,
  $format 
)
static

Extract part of the filesystem path.

Example: Str::extractPath($path, "%f.%e"); //extract "filename.extension"

Parameters
string$pathFilesystem path
string$formatUse following placeholders: d for directory, f filename, e extension.
Returns
string $path

◆ filter()

static filter (   $str,
  $preserve 
)
static

Preserve only specified characters in $str.

Parameters
string$strSource string
string$preserveWhich characters should be preserved (regex) e.g. "\w"

◆ format()

static format (   $str,
array  $params,
  $keepEmpty = false 
)
static

Replace {param} placeholders in string with values from array $param.

Example: print Str::format("Hello {name}", ['name' => 'World']); If $params is two-dimensional array of n-rows, $str is written for each row.

Parameters
string$strstring with {param} placeholders
array$paramsarray of PARAM=>VALUE or array of rows
bool$keepEmptyKeep (don't delete) tags undefined in $param
Returns
string $str

◆ htmlspecialchars()

static htmlspecialchars (   $str)
static

◆ htmlTag()

static htmlTag (   $name,
array  $attr = [],
  $content = null 
)
static

Create html tag.

Example: Str::htmlTag('a', ['href' => 'www.example.com'], 'Example');

Parameters
string$nameName of the tag
string$attrTag attributes
Returns
string $content Content of the tag

◆ id()

static id (   $str,
  $preserve = '\w',
  $separator = '_' 
)
static

Remove diacriticts and non-alphanum characters from $str and replace spaces with '_'.

Result can be used as identificator: filename, database tablename etc.

Parameters
string$strSource string
string$preserveWhich characters should be preserved (regex)
$separatorSeparator of words, '_' by default

◆ length()

static length (   $str)
static
See also
mb_strlen()

◆ lower()

static lower (   $str)
static
See also
mb_strtolower()

◆ lpad()

static lpad (   $str,
  $length,
  $pad = ' ' 
)
static
See also
str_pad()

◆ match()

static match (   $str,
  $pattern 
)
static

Match $pattern on $str and return result or empty string.

Parameters
string$str
string$patternRegexp pattern
Returns
string $str Matched string

◆ matchAll()

static matchAll (   $str,
  $pattern 
)
static

Match $pattern on $str and return all results as array.

Parameters
string$str
string$patternRegexp pattern
Returns
array $results

◆ random()

static random (   $size = 16,
  $characters = self::ALPHANUM 
)
static

Return random string with $size characters.

Parameters
int$sizeNumber of characters
string$charactersSource characters (You can use Str::ALPHASPEC or Str::APLHANUM constants)
Returns
string $str Random string

◆ replace()

static replace (   $str,
  $search,
  $replace 
)
static

Replace all occurences of $search in $str with $replace.

Parameters
string$searchRegexp pattern
string$str
string$replace

◆ rpad()

static rpad (   $str,
  $length,
  $pad = ' ' 
)
static
See also
str_pad()

◆ strpos()

static strpos (   $str,
  $search,
  $offset = 0 
)
static
See also
mb_strpos()

◆ substr()

static substr (   $str,
  $start,
  $length = null 
)
static
See also
mb_substr()

◆ upper()

static upper (   $str)
static
See also
mb_strtoupper()

◆ webalize()

static webalize (   $str)
static

Transform string $str into url-ready form 'some-string-1234'.

See also
Str::id()

The documentation for this class was generated from the following file: