PHP string library

While working with PHP on text-intensive applications I noticed that PHP hasn’t specific functions to solve simple string operations. I decided to write a little library that includes some useful functions for string manipulation.

download: latest version (1.0.0)


str_analyzedt($seperator, $str)


desc

This function analyzes the datatype of every part of the $str exploded by the $seperator. It returns an array with indexes 0 for the word and 1 for the datatype.

param

  • seperator – a token that is used to explode $str
  • str – the string to analyze

return

  • an empty array – when $str is empty or the $seperator doesn’t exist
  • array with data – every string to analyze as an array(0 => string , 1 => datatype)

str_avgsentencelen($str, $whitespaces = false)


desc

This function calculates the average sentence length.

param

  • str – string to analyze
  • whitespaces – ignore whitespaces (true or false)

return

  • integer – with the average sentence length

str_avgwordlen($str)


desc

This function calculates the average word length.

param

  • str – string to analyze

return

  • integer – with the average word length

str_contains($haystack, $needle, $num = true)


desc

This function checks how many times $haystack contains the $needle.

param

  • needle – search string
  • haystack – super string
  • num – return the number of appearances set it to true, set false if you want simply true or false

return

  • true – if you choose num = false and $haystack contains $needle
  • integer – number of appearances in $haystack
  • false – if you choose num = false and $haystack doesn’t contains $needle

str_endswith($haystack, $needle)


desc

This function checks whether $haystack ends with $needle or not.

param

  • needle – search string
  • haystack – super string

return

  • true – if $haystack ends with $needle
  • false  – if $haystack not ends with $needle

str_isword($haystack, $needle)


desc

This function checks whether $needle is a word in $haystack or not.

param

  • needle – search string
  • haystack – super string

return

  • true – if $needle is a word in $haystack
  • false – if $needle isn’t a word in $haystack

str_ndxcontains($haystack, $needle)


desc

This function returns an array with the indexes of all appearances of $needle in $haystack.

param

  • $needle – search string
  • $haystack – super string

return

  • array – all the indexes appearances of $needle in $haystack

str_ndxlen($tok, $str)


desc

This function returns the remaining string length of $string after the appearance of $tok. (first appearance)

param

  • tok – token to search
  • str – super string

return

  • integer – remaining tokens
  • false – if no $tok was found in $str

str_ndxwordstarts($haystack, $needle)


desc

This function returns the index where the word that contains the $needle string starts.

param

  • needle – search string
  • haystack – super string

return

  • integer – index where the word started
  • false – if $needle is not in $haystack

str_rndxlen($tok, $str)


desc

This function returns the string length of $str after the last appearance of $tok.

param

  • tok – token to search
  • str – super string

return

  • integer – the number of remaining tokens from $tok (search started from the end)

str_subxy($haystack, $needle, $left, $right)


desc

This function returns $needle in $haystack with an offset on the right ($right) and left ($left) side.

param

  • needle – search string
  • haystack – super string
  • left – left offset
  • right – right offset

return

  • string – sub string
  • false – $needle not found in $haystack

str_startswith($haystack, $needle)


desc

This function checks whether $haystack starts with $needle or not.

param

  • needle – search string
  • haystack – super string

return

  • true – $haystack starts with $needle
  • false – $haystack doesn’t starts with $needle

str_wordcontains($haystack, $needle)


desc

This function returns an array with all words (in $haystack) that contains $needle.

param

  • needle – search string
  • haystack – super string

return

  • array – array with all appearances words  $needle (array(‘start’ => <start_position>, ‘end’ => <end_position>, ‘word’ => <word>))

str_wordlen($seperator, $str)


desc

This functions seperates the string ($str) with a token ($seperator) and returns an array with the words and the word length.

param

  • seperator – token to explode $str
  • str – super string

return

  • array – with the word length of all elements (array(‘word’ => <word>, ‘length’ => <word_length>))

str_xendswith($array, $haystack)


desc

This function checks whether $haystack ends with one element of $array or not.

param

  • array – possible endings for $haystack
  • haystack – super string

return

  • true – if $haystack ends with one element of $array
  • false – if $haystack doesn’t ends with one element of $array

str_xstartswith($array, $haystack)


desc

This function checks whether $haystack starts with one element of $array or not.

param

  • array – possible start strings of $haystack
  • haystack – super string

return

  • true – if $haystack starts with one element of $array
  • false – if $haystack doesn’t starts with one element of $array

One thought on “PHP string library

Comments are closed.