PKPString::substr PHP Method

substr() static public method

See also: http://ca.php.net/manual/en/function.substr.php
static public substr ( $string, $start, $length = false ) : string
$string string Subject to extract substring from
$start int Position to start from
$length int Length to extract, or false for entire string from start position
return string Substring of $string
    static function substr($string, $start, $length = false)
    {
        if (defined('ENABLE_MBSTRING')) {
            require_once './lib/pkp/lib/phputf8/mbstring/core.php';
        } else {
            require_once './lib/pkp/lib/phputf8/utils/unicode.php';
            require_once './lib/pkp/lib/phputf8/native/core.php';
            // The default length value for the native implementation
            // differs
            if ($length === false) {
                $length = null;
            }
        }
        return utf8_substr($string, $start, $length);
    }

Usage Example

 /**
  * Generate a filename for a library file.
  * @param $type int LIBRARY_FILE_TYPE_...
  * @param $originalFileName string
  * @return string
  */
 function generateFileName($type, $originalFileName)
 {
     $libraryFileDao = DAORegistry::getDAO('LibraryFileDAO');
     $suffix = $this->getFileSuffixFromType($type);
     $ext = $this->getExtension($originalFileName);
     $truncated = $this->truncateFileName($originalFileName, 127 - PKPString::strlen($suffix) - 1);
     $baseName = PKPString::substr($truncated, 0, PKPString::strpos($originalFileName, $ext) - 1);
     // Try a simple syntax first
     $fileName = $baseName . '-' . $suffix . '.' . $ext;
     if (!$libraryFileDao->filenameExists($this->contextId, $fileName)) {
         return $fileName;
     }
     for ($i = 1;; $i++) {
         $fullSuffix = $suffix . '-' . $i;
         //truncate more if necessary
         $truncated = $this->truncateFileName($originalFileName, 127 - PKPString::strlen($fullSuffix) - 1);
         // get the base name and append the suffix
         $baseName = PKPString::substr($truncated, 0, PKPString::strpos($originalFileName, $ext) - 1);
         //try the following
         $fileName = $baseName . '-' . $fullSuffix . '.' . $ext;
         if (!$libraryFileDao->filenameExists($this->contextId, $fileName)) {
             return $fileName;
         }
     }
 }
All Usage Examples Of PKPString::substr