3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Filename { /** * @var string */ private $basename; /** * @var string */ private $extension; /** * Filename constructor. * * @param string $filename * @param array $replace */ public function __construct(string $filename, array $replace = []) { $ext = pathinfo($filename, PATHINFO_EXTENSION); $this->setExtension($ext); if (empty($replace)) { $replace = [ ' ' => '_', 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss' ]; } $this->basename = self::clean(substr($filename, -strlen($ext)), $replace); } /** * @param int $length * * @throws \Exception */ public function limitLength(int $length) { assure($length > 0, 'Invalide Länge: %d', $length); if (strlen($this->basename) > $length) { $this->basename = substr($this->basename, 0, $length); } } /** * @param string $str * @param array $replace * * @return string */ public static function clean(string $str, array $replace = []): string { $str = trim($str); $str = trim($str, '_-'); if (!empty($replace)) { $str = str_replace(array_keys($replace), array_values($replace), $str); } $str = preg_replace('#[^\w\-]+#i', '_', $str); //Mehrere aufeinanderfolgende Unterstriche zu einem Unterstrich $str = preg_replace('#_+#', '_', $str); //Bindestrich vor Unterstrich zum Bindestrich $str = preg_replace('#\-_#', '-', $str); //Bindestrich nach Unterstrich zum Bindestrich $str = preg_replace('#_\-#', '-', $str); //Unterstrich am Ende zu nix $str = preg_replace('#_$#', '', $str); return $str; } /** * @return null|string */ public function getExtension() { return $this->extension; } /** * @return bool */ public function hasExtension(): bool { return $this->extension !== null; } /** * @param string $extension */ public function setExtension(string $extension) { if (MimeTypes::isExtensionSupported($extension)) { $this->extension = $extension; } } /** * @return string */ public function getBasename(): string { return $this->basename; } /** * @return bool */ public function isValid(): bool { return strlen($this->getBasename()) !== 0 && $this->hasExtension(); } /** * @return string */ public function assemble(): string { if ($this->hasExtension()) { return sprintf('%s.%s', $this->getBasename(), $this->getExtension()); } return $this->getBasename(); } } $file = 'Versicherungsschein/Nachträge'; var_dump(new Filename($file));
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught Error: Class "MimeTypes" not found in /in/LpL2t:104 Stack trace: #0 /in/LpL2t(23): Filename->setExtension('') #1 /in/LpL2t(139): Filename->__construct('Versicherungssc...') #2 {main} thrown in /in/LpL2t on line 104
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught Error: Class "MimeTypes" not found in /in/LpL2t:104 Stack trace: #0 /in/LpL2t(23): Filename->setExtension('') #1 /in/LpL2t(139): Filename->__construct('Versicherungssc...') #2 {main} thrown in /in/LpL2t on line 104
Process exited with code 255.
Output for 7.0.0 - 7.0.31, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Class 'MimeTypes' not found in /in/LpL2t:104 Stack trace: #0 /in/LpL2t(23): Filename->setExtension('') #1 /in/LpL2t(139): Filename->__construct('Versicherungssc...') #2 {main} thrown in /in/LpL2t on line 104
Process exited with code 255.

preferences:
184.87 ms | 402 KiB | 239 Q