Location: PHPKode > projects > PWF > PWF_0.3.0/String.php
<?php
/**
 * _String class contains static methods for common string operations
 * @copyright	Copyright (c) 2012, PWF
 * @license		http://phpwebframework.com/License 
 * @version PWF_0.3.0
 */
class _String
{

  /**
   * Checks if a string contains a substring 
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for
   * @return boolean
   */
  public static function contains($string,$search)
  {
    return mb_substr_count($string,$search) > 0?true:false;
  }

  /**
   * Splits a string in parts using a delimiter 
   * @param <b>$string</b> The string to be splitted 
   * @param <b>$delimiter</b> The delimiter for splitting the string 
   * @return array
   */
  public static function split($string,$delimiter)
  {
    return mb_split($delimiter, $string);
  }

  /**
   * Returns how many characters the string is 
   * @param <b>$string</b> The string 
   * @return number
   */
  public static function length($string)
  {
    return mb_strlen($string);
  }

  /**
   * Returns how many length the string is 
   * @param <b>$string</b> The string 
   * @return number
   */
  public static function bytesLength($string)
  {
    return mb_strwidth($string);
  }

  /**
   * Splits a string to its characters
   * @param <b>$string</b> The string 
   * @return array
   */
  public static function toCharacters($string)
  {
    return preg_split('/(?<!^)(?!$)/u', $string );
  }

  /**
   * Strip whitespaces from the beginning or the end of the string 
   * @param <b>$string</b> The string 
   * @param <b<$charlist :optional</b> The stripped characters
   * @see http://php.net/manual/en/function.trim.php
   * @return string
   */
  public static function trim($string,$charlist = null)
  {
    return $charlist == null ? trim($string) : trim($string,$charlist);
  }

  /**
   * Returns a part of the string from a character and for as many characters as provided 
   * @param <b>$string</b> The string to retrieve the part from
   * @param <b>$start</b> The index of the first character (0 for the first one) 
   * @param <b>$length</b> The length of the part the will be extracted from the string 
   * @return string
   */
  public static function substring($string,$start,$length = null)
  {
    return ( $length == null ) ? ( mb_substr($string, $start) ) : ( $length == 0 ? "" : mb_substr($string, $start , $length) );
  }

  /**
   * Return the index of <b>the first occurance</b> of a part of a string to the string 
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return number
   */
  public static function firstIndexOf($string,$search,$caseSensitive = true)
  {
    return $caseSensitive ? mb_strpos($string, $search) : mb_stripos($string, $search);
  }

  /**
   * Return the index of <b>the last occurance</b> of a part of a string to the string 
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return number
   */
  public static function lastIndexOf($string,$search,$caseSensitive = true)
  {
    return $caseSensitive ? mb_strrpos($string, $search) : mb_strripos($string, $search);
  }

  /**
   * Returns the part of a string <b>before the first</b> occurrence of the string to search for. 
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return string
   */
  public static function beforeFirst($string,$search,$caseSensitive = true)
  {
    $firstIndex = self::firstIndexOf($string, $search,$caseSensitive);
    return $firstIndex == 0 ? "" : self::substring($string, 0 , $firstIndex);
  }

  /**
   * Returns the part of a string <b>after the first</b> occurrence of the string to search for. 
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return string
   */
  public static function afterFirst($string,$search,$caseSensitive = true)
  {
    return self::substring($string, self::firstIndexOf($string, $search,$caseSensitive) + self::length($search));
  }

  /**
   * Returns the part of a string <b>before the last</b> occurrence of the string to search for. 
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return string
   */
  public static function beforeLast($string,$search,$caseSensitive = true)
  {
    return self::substring($string, 0 , self::lastIndexOf($string, $search,$caseSensitive));
  }

  /**
   * Returns the part of a string <b>after the last</b> occurrence of the string to search for. 
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return string
   */
  public static function afterLast($string,$search,$caseSensitive = true)
  {
    return self::substring($string, self::lastIndexOf($string, $search,$caseSensitive) + self::length($search));
  }

  /**
   * Replaces parts of a string that matches the string to search for
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for 
   * @param <b>$replace</b> The string to replace the occurances of the search string 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return string 
   */
  public static function replace($string,$search,$replace,$caseSensitive = true)
  {
    return $caseSensitive?str_replace($search, $replace, $string):str_ireplace($search, $replace, $string);
  }

  /**
   * Replaces <b>only the first</b> part of a string that matches the string to search for
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for 
   * @param <b>$replace</b> The string to replace the first occurance of the search string 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return string
   */
  public static function replaceFirst($string,$search,$replace,$caseSensitive = true)
  {
    return self::contains($string, $search) ? self::beforeFirst($string, $search,$caseSensitive).$replace.self::afterFirst($string, $search,$caseSensitive) : $string;
  }

  /**
   * Replaces <b>only the last</b> part of a string that matches the string to search for
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for 
   * @param <b>$replace</b> The string to replace the last occurance of the search string 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return string
   */
  public static function replaceLast($string,$search,$replace,$caseSensitive = true)
  {
    return self::contains($string, $search) ? self::beforeLast($string, $search,$caseSensitive).$search.self::afterLast($string, $search,$caseSensitive) : $string;
  }

  /**
   * Counts the occurrences of a string inside another string and returns the result 
   * @param <b>$string</b> The string to be searched 
   * @param <b>$search</b> The string to search for 
   * @return number
   */
  public static function count($string,$search)
  {
    return mb_substr_count($string,$search);
  }

  /**
   * Prepares and return a string containing question marks replacing them with string given
   * @param <b>$string</b> The string to prepare 
   * @param <b>$replacements</b> The replacement strings of question marks occurrences of 
   * the string. Can be string if there is one question mark otherwise array with the 
   * replacement strings sized the number of ? occurrences
   * @return string
   */
  public static function prepare($string,$replacements)
  {
    $string = self::replace($string, "\?", "[<(PWF TEMP REPLACEMENT QUESTION MARK)>]");
    if(!_Array::isValid($replacements))
    {
      $replacements = array($replacements);
    }
    foreach($replacements as $replace)
    {
      $string = self::replaceFirst($string, "?", $replace);
    }
    return self::replace($string, "[<(PWF TEMP REPLACEMENT QUESTION MARK)>]", "?");
  }

  /**
   *Checks if two strings are the same  
   * @param <b>$string1</b> The first string to check
   * @param <b>$string2</b> The second string to check
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return boolean
   */
  public static function equals($string1,$string2,$caseSensitive = true)
  {
    return $caseSensitive ? $string1 == $string2 : self::toLowerCase($string1) == self::toLowerCase($string2);
  }

  /**
   * Checks if a string <b>starts with</b> one other string   
   * @param <b>$string</b> The string to be checked 
   * @param <b>$search</b> The string to check for 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return boolean
   */
  public static function startsWith($string,$search,$caseSensitive = true)
  {
    return self::length($search) > self::length($string) ? false : self::equals(self::substring($string, 0,self::length($search)),$search,$caseSensitive);
  }

  /**
   * Checks if a string <b>ends with</b> one other string   
   * @param <b>$string</b> The string to be checked 
   * @param <b>$search</b> The string to check for 
   * @param <b>$caseSensitive boolean :optional</b> Defines if the search will be case sensitive. By default true.
   * @return boolean
   */
  public static function endsWith($string,$search,$caseSensitive = true)
  {
    return self::length($search) > self::length($string) ? false : self::equals(self::substring($string, self::length($string) - self::length($search)),$search,$caseSensitive);
  }

  /**
   * Encodes a string to html special characters 
   * @param <b>$string</b> The sting to be encoded
   * @see http://php.net/manual/en/function.htmlentities.php
   * @return string
   */
  public static function encodeHtmlCharacters($string)
  {
    return htmlentities($string);
  }

  /**
   * Decodes a string from html special characters 
   * @param <b>$string</b> The sting to be decoded
   * @see http://www.php.net/manual/en/function.html-entity-decode.php
   * @return string
   */
  public static function decodeHtmlCharacters($stirng)
  {
    return html_entity_decode($string);
  }

  /**
   * Removes everything in a string that is inside tags    
   * @param <b>$string</b> The string to reomve tags from 
   * @see http://php.net/manual/en/function.strip-tags.php
   * @return string
   */
  public static function removeTags($string)
  {
    return strip_tags($string);
  }

  /**
   * Takes a string and creates an insensitive case pattern as needed for file search functions.     
   * @param <b>$string</b> The string that will create the pattern 
   * @return string 
   */
  public static function insesitiveCasePattern($string)
  {
    $letters = self::toCharacters($string);
    $result = "";
    foreach ($letters as $letter)
    {
      $lowerCase = self::toLowerCase($letter);
      $upperCase = self::toUpperCase($letter);
      if($lowerCase == $upperCase)
      {
        $result .= $letter;
      }
      else
      {
        $result .= "[".$lowerCase.$upperCase."]";
      }
    }
    return $result;
  }

  /**
   * Converts a string to lower case
   * @param <b>$string</b> The sting to be converted 
   * @return string
   */
  public static function toLowerCase($string)
  {
    return mb_strtolower($string);
  }

  /**
   * Converts a string to upper case
   * @param <b>$string</b> The string to be converted 
   * @return string
   */
  public static function toUpperCase($string)
  {
    return mb_strtoupper($string);
  }

  /**
   * It makes uppercase the first letter of a string
   * @param <b>$string</b> The string
   * @return string
   */
  public static function capitalizeFirstLetter($string)
  {
    return self::length($string) < 2 ? self::toUpperCase($string) : self::toUpperCase(self::substring($string, 0,1)) . self::substring($string, 1);
  }

  /**
   * It makes uppercase the first letter of each word in a string
   * @param unknown_type $string
   * @return string
   */
  public static function capitalizeWordsFirstLetter($string)
  {
    return mb_convert_case($string, MB_CASE_TITLE);
  }

  public static function unify($string)
  {
    $result = "";
    $words = self::split($string, " ");
    foreach ($words as $word)
    {
      $word = _String::trim($word);
      if($word != "")
      {
        $result .= $word." ";
      }
    }
    return _String::trim($result);
  }

  public static function toLine($string,$lineLength,$unify = true)
  {
    if($unify)
    {
      $string = self::unify($string);
    }

    if(self::length($string) <= $lineLength)
    {
      $line =  $string;
    }
    else
    {
      $o = 0;
      $line = "";
      $words = self::split($string, " ");
      $continue = true;
      $wordToAdd = $words[0];
      while($continue && $o < _Array::size($words))
      {
        $newLine = $line." ".$words[$o];
        if(_String::length($newLine) < $lineLength)
        {
          $line = $newLine;
          $o++;
        }
        else
        {
          $continue = false;
        }
      }
      return _String::trim($line);
    }

  }

  public static function toLines($string,$lineLength,$unify = true)
  {
    if($unify)
    {
      $string = self::unify($string);
    }
    $lines = array();
    if(self::length($string) <= $lineLength)
    {
      $lines[] =  $string;
    }
    else
    {
      $i = 0;
      $o = 0;
      $words = self::split($string, " ");
      $lines[$i] = "";
      $wordToAdd = $words[0];
      while($o < _Array::size($words))
      {

        $currentLineLength = self::length($lines[$i]);
        if(self::length($wordToAdd) >= $lineLength)
        {
          $lines[$i] = $lines[$i]." ".self::substring($wordToAdd, 0,$lineLength - $currentLineLength);
          $wordToAdd = self::substring($wordToAdd,$lineLength - $currentLineLength);
          $lines[$i] = _String::trim($lines[$i]);
          $i++;
          $lines[$i] = "";
        }
        else if(self::length($wordToAdd) +  $currentLineLength >= $lineLength)
        {
          $lines[$i] = _String::trim($lines[$i]);
          $i++;
          $lines[$i] = "";
        }
        else
        {
          $lines[$i] = $lines[$i]." ".$wordToAdd;
          $o++;
          if($o < _Array::size($words))
          {
            $wordToAdd = $words[$o];
          }
          else
          {
            $lines[$i] = _String::trim($lines[$i]);
          }
        }
      }
    }
    return $lines;
  }

	/**
	 * Encodes the given string according to url
	 * @param <b>$string</b> The string 
	 * @return string
	 */
	public static function urlEncode($string)
	{
		return rawurlencode($string);
	}

	/**
	 * Decodes the given string according to url
	 * @param <b>$string</b> The string 
	 * @return string
	 */
	public static function urlDecode($string)
	{
		return rawurldecode($string);
	}
	
	/**
	 * Returns if the given string is an integer 
	 * @param string $string
	 * @return boolean
	 */
	public static function isInteger($string)
	{
		return preg_match('/(?<!\S)\d++(?!\S)/', $string)?true:false; 
	}
	
	/**
	 * Returns if the given string is an e-mail 
	 * @param string $string
	 * @return boolean
	 */
	public static function isEmail($string)
	{
		return preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/', _String::toUpperCase($string))?true:false; 
	}	
}
?>
Return current item: PWF