Example #1
0
 public function apply($value)
 {
     $result = basename((string) $value);
     $result = preg_replace('@[/?*:;{}\\\\]@', '', $result);
     if ($this->paranoid) {
         $result = preg_replace('@[^A-Z0-9_.-]@i', '', $result);
     }
     if (String::length($result) > $this->maxLength) {
         if (($dotPos = strpos($result, '.')) !== false) {
             $extension = substr($result, $dotPos + 1);
             $basename = substr($result, 0, $dotPos);
             $result = String::substr($basename, 0, $this->maxLength - String::length($extension) - 1) . '.' . $extension;
         } else {
             $result = String::substr($result, 0, $this->maxLength);
         }
     }
     return $result;
 }
Example #2
0
 public function testSubstr()
 {
     $t = 'Waschbär';
     $this->assertEquals(String::substr($t, 0), $t);
     $this->assertEquals(String::substr($t, 1), 'aschbär');
     $this->assertEquals(String::substr($t, 6), 'är');
     $this->assertEquals(String::substr($t, 0, 1), 'W');
     $this->assertEquals(String::substr($t, 6, 1), 'ä');
     $this->assertEquals(String::substr($t, -2, 1), 'ä');
     $this->assertEquals(String::substr($t, -2), 'är');
     $this->assertEquals(String::substr($t, -2, 0), '');
     $this->assertEquals(String::substr($t, 0, 0), '');
     $this->assertEquals(String::substr($t, 1, 0), '');
     $this->assertEquals(String::substr(chr(0xe4) . chr(0xf6) . chr(0xe4), 1, 1), chr(0xf6));
     $this->assertEquals(String::substr(chr(0xe4) . chr(0xf6) . chr(0xe4), 1), chr(0xf6) . chr(0xe4));
 }