Backend\Core\Engine\DataGridFunctions::truncate PHP Method

truncate() public static method

Truncate a string
public static truncate ( string $string, integer $length, boolean $useHellip = true ) : string
$string string The string to truncate.
$length integer The maximumlength for the string.
$useHellip boolean Should a hellip be appended?
return string
    public static function truncate($string, $length, $useHellip = true)
    {
        // remove special chars
        $string = htmlspecialchars_decode($string);
        // less characters
        if (mb_strlen($string) <= $length) {
            return \SpoonFilter::htmlspecialchars($string);
        } else {
            // more characters
            // hellip is seen as 1 char, so remove it from length
            if ($useHellip) {
                --$length;
            }
            // get the amount of requested characters
            $string = mb_substr($string, 0, $length);
            // add hellip
            if ($useHellip) {
                $string .= '…';
            }
            return \SpoonFilter::htmlspecialchars($string);
        }
    }

Usage Example

Example #1
0
 /**
  * Get modules based on the directory listing in the backend application.
  *
  * If a module contains a info.xml it will be parsed.
  *
  * @return array
  */
 public static function getModules()
 {
     $installedModules = (array) BackendModel::getContainer()->getParameter('installed_modules');
     $modules = BackendModel::getModulesOnFilesystem(false);
     $manageableModules = array();
     // get more information for each module
     foreach ($modules as $moduleName) {
         if (in_array($moduleName, self::$ignoredModules)) {
             continue;
         }
         $module = array();
         $module['id'] = 'module_' . $moduleName;
         $module['raw_name'] = $moduleName;
         $module['name'] = \SpoonFilter::ucfirst(BL::getLabel(\SpoonFilter::toCamelCase($moduleName)));
         $module['description'] = '';
         $module['version'] = '';
         $module['installed'] = false;
         $module['cronjobs_active'] = true;
         if (in_array($moduleName, $installedModules)) {
             $module['installed'] = true;
         }
         try {
             $infoXml = @new \SimpleXMLElement(BACKEND_MODULES_PATH . '/' . $module['raw_name'] . '/info.xml', LIBXML_NOCDATA, true);
             $info = self::processModuleXml($infoXml);
             // set fields if they were found in the XML
             if (isset($info['description'])) {
                 $module['description'] = BackendDataGridFunctions::truncate($info['description'], 80);
             }
             if (isset($info['version'])) {
                 $module['version'] = $info['version'];
             }
             // check if cronjobs are set
             if (isset($info['cronjobs'])) {
                 foreach ($info['cronjobs'] as $cronjob) {
                     if (!$cronjob['active']) {
                         $module['cronjobs_active'] = false;
                         break;
                     }
                 }
             }
         } catch (\Exception $e) {
             // don't act upon error, we simply won't possess some info
         }
         $manageableModules[] = $module;
     }
     return $manageableModules;
 }