ACC SHELL
<?php
/**
* trida pro praci s retezcem
*
* @since 3.11.11 8:50
* @author Vlahovic
*/
class string{
private $origin;
private $string;
private $striped_string;
private $suffix='...';
private $length=150;
private $hard_cut=false;
private $max_word_length=30;
private $return_origin=true;
private $strip_tags=true;
private $one_letter_strings=array('a','i','o','u','s','v','z');
/**
* nastaveni hodnot, se kterymi se bude pracovat
*
* @param string $string zpracovavany retezec
* @param array $sett array(
* suffix,
* length,
* hard_cut,
* max_word_length,
* return_origin,
* strip_tags
* )
* @since 3.11.11 8:50
* @author Vlahovic
*/
public function __construct($string=false,$sett=array()){
$this->set_string($string);
$this->origin=$string;
$this->striped_string=strip_tags($string);
// prochazi pole s nastavenim
if(is_array($sett) && count($sett)){
foreach($sett as $attr => $value){
// pokud existuje prislusna metoda pak atribut nastavi
$method='set_'.$attr;
if(method_exists($this, $method)){
$this->$method($value);
}
}
}
}
public function __toSting(){
}
/**
* retezec se kterym bude objekt pracovat
*
* @param string $string
* @since 3.11.11 8:32
* @author Vlahovic
*/
final public function set_string($string){
$this->string=$string;
}
/**
* retezec, ktery se pripoji na konec vystupniho retezce
* u metod, ktere jej maji pripojovat
*
* @param string $string
* @since 3.11.11 8:32
* @author Vlahovic
*/
final public function set_suffix($suffix){
$this->suffix=$suffix;
}
/**
* maximalni delka retezce
* metody, ktere retezec zkracuji budou pracovat s timto cislem
*
* @param integer $length
* @since 3.11.11 8:39
* @author Vlahovic
*/
final public function set_length($length){
$this->length=intval($length);
}
/**
* kdyz je true, tak metody, ktere retezec zkracuji jej useknou bez ohledu na slova, nebo tagy
*
* @param boolean $hard_cut
* @since 3.11.11 8:46
* @author Vlahovic
*/
final public function set_hard_cut($hard_cut){
$this->hard_cut=($hard_cut || $hard_cut=='on' ? true : false);
}
/**
* maximalni delka slova
* pokud bude existovat delsi a bude potraba v nem retezec zkratit, pak se to udela
*
* @param integer $max_word_length
* @since 3.11.11 8:53
* @author Vlahovic
*/
final public function set_max_word_length($max_word_length){
$this->max_word_length=intval($max_word_length);
}
/**
* zda se ma vratit puvodni retezec v pripade, ze vysledek je prazdny
*
* @param type $return_origin
* @since 3.11.11 9:16
* @author Vlahovic
*/
final public function set_return_origin($return_origin){
$this->return_origin=($return_origin || $return_origin=='on' ? true : false);
}
/**
* zda se maji z retezce odstranit tagy
*
* @param type $strip_tags
* @since 3.11.11 9:16
* @author Vlahovic
*/
final public function set_strip_tags($strip_tags){
$this->strip_tags=($strip_tags || $strip_tags=='on' ? true : false);
}
/**
* zkrati dodany retezec na pozadovanou delku
*
* @return string
* @since 1.11.11 12:16
* @author Vlahovic
*/
public function cut(){
$return=false;
$working_length=0;
$string=($this->strip_tags ? $this->striped_string : $this->string);
if($string && strlen($string) && $this->length){
$return=array();
$working_string=str_replace(' ', ' ',$string);
$expl=explode(' ',$working_string);
$length=$this->length();
// pracuje jen pokud je retezec delsi nez je maximalni delka
if($length>$this->length){
foreach($expl as $word){
$strlen=strlen($word);
$rest=$this->max_word_length-$working_length;
// natvrdo usekne
// pokud je to nastaveno
// nebo pokud je delka vetsi nez povolena delka slova
// a pretece tim maximalni delka
if(($working_length+$strlen)>$this->length && ($this->hard_cut || ($this->return_origin<$strlen)) && $rest>0){
$return[]=substr(0,$rest,$word);
break;
}
// prida slovo k vystupu
$return[]=$word;
// pocita delku retezce, jednicka je tam kvuli mezislovni mezere
// ktera se nakonci prida ke kazdemu slovu
$working_length+=$strlen+1;
// pokud delka presahne povolenou tak ukonci cyklus
if($working_length>$this->length){
break;
}
}
$return=implode(' ',$return);
// pokud se retezec zkratil, tak pripoji suffix
$return=($length!=$this->length($return) ? $return.$this->suffix : $return);
}
// retezec neni potreba zkracovat
else{
$return=$string;
}
}
$this->set_working($return);
return $this->ret();
}
/**
* osetri jednoznakova slova tak, aby nebyla koncich radku
* @return string
* @since 3.11.11 10:21
* @author Vlahovic
*/
public function one_letter_string(){
$from=array();
$to=array();
foreach($this->one_letter_strings as $letter){
$from[]=' '.strtoupper($letter).' ';
$from[]=' '.strtolower($letter).' ';
$to[]=' '.strtoupper($letter).' ';
$to[]=' '.strtolower($letter).' ';
}
if($this->strip_tags){
$this->striped_string=str_replace($from,$to,$this->striped_string);
}
else{
$this->string=str_replace($from,$to,$this->string);
}
return $this->ret();
}
/**
* vraci delku retezce se kterym se prave pracuje
* nebo toho, ktery dojde v parametru
*
* @param string $string [optional]
* @return integer
* @since 3.11.11 14:32
* @author Vlahovic
*/
public function length($string=false){
if($string){
return strlen($string);
}
return strlen($this->get_working());
}
/**
* vraci retezec na kterem se pracuje
*
* @return string
* @since 3.11.11 14:27
* @author Vlahovic
*/
private function get_working(){
$string=false;
if($this->strip_tags){
$string=$this->striped_string;
}
else{
$string=$this->string;
}
return $string;
}
/**
* ulozi retezec do toho s nimz se prave pracuje
*
* @param string $string
* @since 3.11.11 14:34
* @author Vlahovic
*/
private function set_working($string){
if($this->strip_tags){
$this->striped_string=$string;
}
else{
$this->string=$string;
}
}
/**
* osetreni vraceni puvodni hodnoty
* @return string
* @since 3.11.11 10:24
* @author Vlahovic
*/
private function ret(){
if($this->strip_tags){
return ($this->striped_string ? $this->striped_string : ($this->return_origin ? $this->origin : false));
}
return ($this->string ? $this->string : ($this->return_origin ? $this->origin : false));
}
public function __toString()
{
return $this->ret();
}
}
?>
ACC SHELL 2018