trim() static public method

It removes double spaces. Can be useful in some cases but be careful as it might remove too much.
static public trim ( string $string ) : string
$string string The string to trim
return string The trimmed string
コード例 #1
0
ファイル: kirby.php プロジェクト: sdvig/kirbycms
 /** 
  * A set of sanitizer methods
  * 
  * @param  string  $string The string to sanitize
  * @param  string  $type The method
  * @param  string  $default The default value if the string will be empty afterwards
  * @return string  The sanitized string
  */
 static function sanitize($string, $type = 'str', $default = null)
 {
     $string = stripslashes((string) $string);
     $string = urldecode($string);
     $string = str::utf8($string);
     switch ($type) {
         case 'int':
             $string = (int) $string;
             break;
         case 'str':
             $string = (string) $string;
             break;
         case 'array':
             $string = (array) $string;
             break;
         case 'nohtml':
             $string = self::unhtml($string);
             break;
         case 'noxml':
             $string = self::unxml($string);
             break;
         case 'enum':
             $string = in_array($string, array('y', 'n')) ? $string : $default;
             $string = in_array($string, array('y', 'n')) ? $string : 'n';
             break;
         case 'checkbox':
             $string = $string == 'on' ? 'y' : 'n';
             break;
         case 'url':
             $string = v::url($string) ? $string : '';
             break;
         case 'email':
             $string = v::email($string) ? $string : '';
             break;
         case 'plain':
             $string = str::unxml($string);
             $string = str::unhtml($string);
             $string = str::trim($string);
             break;
         case 'lower':
             $string = str::lower($string);
             break;
         case 'upper':
             $string = str::upper($string);
             break;
         case 'words':
             $string = str::sanitize($string, 'plain');
             $string = preg_replace('/[^\\pL]/u', ' ', $string);
         case 'tags':
             $string = str::sanitize($string, 'plain');
             $string = preg_replace('/[^\\pL\\pN]/u', ' ', $string);
             $string = str::trim($string);
         case 'nobreaks':
             $string = str_replace('\\n', '', $string);
             $string = str_replace('\\r', '', $string);
             $string = str_replace('\\t', '', $string);
             break;
         case 'url':
             $string = self::urlify($string);
             break;
         case 'filename':
             $string = f::safe_name($string);
             break;
     }
     return trim($string);
 }
コード例 #2
0
 function languageSetup()
 {
     // check for activated language support
     if (!c::get('lang.support')) {
         return false;
     }
     // get the available languages
     $available = c::get('lang.available');
     // sanitize the available languages
     if (!is_array($available)) {
         // switch off language support
         c::set('lang.support', false);
         return false;
     }
     // get the raw uri
     $uri = uri::raw();
     // get the current language code
     $code = a::first(explode('/', $uri));
     // try to detect the language code if the code is empty
     if (empty($code)) {
         if (c::get('lang.detect')) {
             // detect the current language
             $detected = str::split(server::get('http_accept_language'), '-');
             $detected = str::trim(a::first($detected));
             $detected = !in_array($detected, $available) ? c::get('lang.default') : $detected;
             // set the detected code as current code
             $code = $detected;
         } else {
             $code = c::get('lang.default');
         }
         // go to the default homepage
         go(url(false, $code));
     }
     // http://yourdomain.com/error
     // will redirect to http://yourdomain.com/en/error
     if ($code == c::get('404')) {
         go(url('error', c::get('lang.default')));
     }
     // validate the code and switch back to the homepage if it is invalid
     if (!in_array($code, c::get('lang.available'))) {
         go(url());
     }
     // set the current language
     c::set('lang.current', $code);
     // mark if this is a translated version or the default version
     $code != c::get('lang.default') ? c::set('lang.translated', true) : c::set('lang.translated', false);
     // load the additional language files if available
     load::language();
 }
コード例 #3
0
ファイル: type.php プロジェクト: amekusa/plz
 /**
  * Evaluates `$X` as a boolean
  *
  * In detail:
  *
  * + If `$X` is an object, calls `$X->toBoolean()` or `$X->toBool()` if they exist.
  * + If `$X` is a countable object, returns whether `count($X) > 0`.
  *
  * @example Number to boolean
  * ```php
  * var_dump( type::bool(1)  ); // Integer
  * var_dump( type::bool(0)  ); // Zero
  * var_dump( type::bool(-1) ); // Negative
  * ```
  * @example String to boolean
  * ```php
  * var_dump( type::bool('string') ); // String
  * var_dump( type::bool('')       ); // Empty String
  * ```
  * @example Array to boolean
  * ```php
  * var_dump( type::bool(array ('A', 'B', 'C')) ); // Array
  * var_dump( type::bool(array ())              ); // Empty Array
  * ```
  * @example Semantic evaluation
  * ```php
  * // A string that is not empty, but "falsy" word is evaluated as false.
  * var_dump( type::bool('false') );
  * var_dump( type::bool('False') );
  * var_dump( type::bool('FALSE') );
  * var_dump( type::bool('null')  );
  * var_dump( type::bool('Null')  );
  * var_dump( type::bool('NULL')  );
  * var_dump( type::bool('no')    );
  * var_dump( type::bool('No')    );
  * var_dump( type::bool('NO')    );
  * var_dump( type::bool('off')   );
  * var_dump( type::bool('Off')   );
  * var_dump( type::bool('OFF')   );
  * ```
  * @example Methods evaluation
  * ```php
  * class Truthy {
  *   function toBool() {
  *     return true;
  *   }
  * }
  *
  * class Falsy {
  *   function toBool() {
  *     return false;
  *   }
  * }
  *
  * $obj1 = new Truthy();
  * $obj2 = new Falsy();
  * var_dump( type::bool($obj1) );
  * var_dump( type::bool($obj2) );
  * ```
  * @param mixed $X A variable to treat as a boolean
  * @param boolean $Alt *(optional)* An alternative value to return if evaluation has failed
  * @return boolean
  */
 static function bool($X, $Alt = false)
 {
     if (is_bool($X)) {
         return $X;
     }
     if (is_string($X)) {
         if (!$X) {
             return false;
         }
         switch (str::trim($X)) {
             case 'false':
             case 'False':
             case 'FALSE':
             case 'null':
             case 'Null':
             case 'NULL':
             case 'no':
             case 'No':
             case 'NO':
             case 'off':
             case 'Off':
             case 'OFF':
                 return false;
         }
     } else {
         if (is_object($X)) {
             if ($X instanceof \Countable) {
                 return $X->count() > 0;
             }
             $r = null;
             if (is_callable(array($X, 'toBoolean'))) {
                 $r = $X->toBoolean();
             } else {
                 if (is_callable(array($X, 'toBool'))) {
                     $r = $X->toBool();
                 }
             }
             if (is_bool($r)) {
                 return $r;
             }
         }
     }
     try {
         return (bool) $X;
     } catch (ErrorException $e) {
         return $Alt;
     }
 }
コード例 #4
0
ファイル: str.php プロジェクト: amekusa/plz
 /**
  * Returns whether `$X` contains any visible character
  * @example Demonstration
  * ```php
  * $var1 = " \t \n ";   // Spaces, Tab, Linebreak
  * $var2 = " \t \n _ "; // Spaces, Tab, Linebreak, and Underscore
  * var_dump( str::is_visible($var1) );
  * var_dump( str::is_visible($var2) );
  * ```
  * @param string $X
  * @return boolean
  */
 static function is_visible($X)
 {
     return !empty(str::trim($X));
 }