URLify::filter PHP Метод

filter() публичный статический Метод

Filters a string, e.g., "Petty theft" to "petty-theft"
public static filter ( string $text, integer $length = 60, string $language = "", boolean $file_name = false, boolean $use_remove_list = true, boolean $lower_case = true, boolean $treat_underscore_as_space = true )
$text string The text to return filtered
$length integer The length (after filtering) of the string to be returned
$language string The transliteration language, passed down to downcode()
$file_name boolean Whether there should be and additional filter considering this is a filename
$use_remove_list boolean Whether you want to remove specific elements previously set in self::$remove_list
$lower_case boolean Whether you want the filter to maintain casing or lowercase everything (default)
$treat_underscore_as_space boolean Treat underscore as space, so it will replaced with "-"
    public static function filter($text, $length = 60, $language = "", $file_name = false, $use_remove_list = true, $lower_case = true, $treat_underscore_as_space = true)
    {
        $text = self::downcode($text, $language);
        if ($use_remove_list) {
            // remove all these words from the string before urlifying
            $text = preg_replace('/\\b(' . join('|', self::$remove_list) . ')\\b/i', '', $text);
        }
        // if downcode doesn't hit, the char will be stripped here
        $remove_pattern = $file_name ? '/[^_\\-.\\-a-zA-Z0-9\\s]/u' : '/[^\\s_\\-a-zA-Z0-9]/u';
        $text = preg_replace($remove_pattern, '', $text);
        // remove unneeded chars
        if ($treat_underscore_as_space) {
            $text = str_replace('_', ' ', $text);
            // treat underscores as spaces
        }
        $text = preg_replace('/^\\s+|\\s+$/u', '', $text);
        // trim leading/trailing spaces
        $text = preg_replace('/[-\\s]+/u', '-', $text);
        // convert spaces to hyphens
        if ($lower_case) {
            $text = strtolower($text);
            // convert to lowercase
        }
        return trim(substr($text, 0, $length), '-');
        // trim to first $length chars
    }

Usage Example

Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function upload($fp, $dst, $name, $tmpname)
 {
     $this->setConnectorFromPlugin();
     // upload file by elfinder.
     $result = parent::upload($fp, $dst, $name, $tmpname);
     $name = $result['name'];
     $filtered = \URLify::filter($result['name'], 80);
     if (strcmp($name, $filtered) != 0) {
         /*$arg = array('target' => $file['hash'], 'name' => $filtered);
           $elFinder->exec('rename', $arg);*/
         $this->rename($result['hash'], $filtered);
     }
     $realPath = $this->realpath($result['hash']);
     if (!empty($realPath)) {
         // Getting file info
         //$info = $elFinder->exec('file', array('target' => $file['hash']));
         /** @var elFinderVolumeLocalFileSystem $volume */
         //$volume = $info['volume'];
         //$root = $volume->root();
         //var/www/chamilogits/data/courses/NEWONE/document
         $realPathRoot = $this->getCourseDocumentSysPath();
         // Removing course path
         $realPath = str_replace($realPathRoot, '/', $realPath);
         \FileManager::add_document($this->connector->course, $realPath, 'file', intval($result['size']), $result['name']);
     }
     return $result;
 }
All Usage Examples Of URLify::filter