<?php
class Pampa_Util_String {

  public static function removeAcentos($str, $enc = 'UTF-8') {

    $acentos = array(
        'A' => '/&Agrave;|&Aacute;|&Acirc;|&Atilde;|&Auml;|&Aring;/',
        'a' => '/&agrave;|&aacute;|&acirc;|&atilde;|&auml;|&aring;/',
        'C' => '/&Ccedil;/',
        'c' => '/&ccedil;/',
        'E' => '/&Egrave;|&Eacute;|&Ecirc;|&Euml;/',
        'e' => '/&egrave;|&eacute;|&ecirc;|&euml;/',
        'I' => '/&Igrave;|&Iacute;|&Icirc;|&Iuml;/',
        'i' => '/&igrave;|&iacute;|&icirc;|&iuml;/',
        'N' => '/&Ntilde;/',
        'n' => '/&ntilde;/',
        'O' => '/&Ograve;|&Oacute;|&Ocirc;|&Otilde;|&Ouml;/',
        'o' => '/&ograve;|&oacute;|&ocirc;|&otilde;|&ouml;/',
        'U' => '/&Ugrave;|&Uacute;|&Ucirc;|&Uuml;/',
        'u' => '/&ugrave;|&uacute;|&ucirc;|&uuml;/',
        'Y' => '/&Yacute;/',
        'y' => '/&yacute;|&yuml;/',
        'a.' => '/&ordf;/',
        'o.' => '/&ordm;/',
        '' => '/&amp;/'
    );

    return preg_replace($acentos, array_keys($acentos), htmlentities($str,ENT_NOQUOTES, $enc));
  }

  public static function alternator() {
    static $i;

    if (func_num_args() == 0) {
      $i = 0;
      return '';
    }
    $args = func_get_args();
    return $args[($i++ % count($args))];
  }

  public static function url($str, $replace=array(), $delimiter='-') {
    /**/
    $displayError = ini_get('display_errors');
    ini_set('display_errors', 0);
    $old_locale = setlocale(LC_ALL, "0");
    setlocale(LC_ALL, 'en_US.UTF8');

    $clean = $str;

    $transcript = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
    $clean = empty($transcript) ? $str : $transcript;

    $replace = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
    $replace = trim($replace);
    $clean =  empty($replace) ? $clean : $replace;

  	$clean = strtolower(trim($clean, '-'));
  	$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

    ini_set('display_errors', $displayError);
    setlocale(LC_ALL, $old_locale);

    return $clean;
    /* *

    $friendlyURL = htmlentities($str, ENT_COMPAT, "UTF-8", false);
    $friendlyURL = preg_replace('/&([a-z]{1,2})(?:acute|lig|grave|ring|tilde|uml|cedil|caron);/i','\1',$friendlyURL);
    $friendlyURL = html_entity_decode($friendlyURL,ENT_COMPAT, "UTF-8");
    $friendlyURL = preg_replace('/[^a-z0-9-]+/i', '-', $friendlyURL);
    $friendlyURL = preg_replace('/-+/', '-', $friendlyURL);
    $friendlyURL = trim($friendlyURL, '-');
    $friendlyURL = strtolower($friendlyURL);
    return $friendlyURL;
    /**/
  }

  public static function crop($string, $length, $spacer = ""){
    $string = trim($string);
    if(strlen($string) > $length){
      $string = trim(substr($string, 0, $length));
      return $string.$spacer;
    }
    return $string;
  }

  public static function checkEmail($email)
  {
    if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
      return true;
    else
      return false;
  }

  public static function sample($text, $length, $spacer = ""){
    $text = trim(strip_tags(stripslashes($text)));
    return self::crop($text, $length, $spacer);
  }
}