<?php
$tests = [
'this is a test! this is a test! this is a test! this is a test! this is a test! cmyk derp.png',
' this is a test! this is a test! this is a test! this is a test! this is a test! cmyk x.png ',
' this is a test! this is a test! this is a test! this is a test! this is a test! cmykx .png ',
' this is a test! this is a test! this is a test! this is a test! this is a test! derpderp .png ',
' this is a test! this is a test! this is a test! this is a test! this is a test! derpd .png ',
' this is a test! this is a test! this is a test! this is a test! this is a test! derp .png ',
' this is a test! this is a test! this is a test! this is a test! this is a test.png ',
'this is a test! this is a test! this is a test! this is a test! this is a - - - - - - test.png',
'this is a test! this is a test! this is a test! this is a test! this is a test! this is a test! this is a test! .png',
'this is a test! this is a test! this is a test! this is a test! this is a test! thisx is a test! this is a test! .png',
'this is a test! this is a test! this is a test! this is a test! this is a test! thisxy is a test! this is a test! .png',
];
foreach ( $tests as $originalFilename) {
$filteredFilename = str_replace(' ', '-', strtolower(trim($originalFilename)));
$filteredFilename = substr( $filteredFilename, 0, strrpos($filteredFilename,'.') );
$filteredFilename = preg_replace('/(\bcmyk\b)|([^a-z0-9\-]+)|(\-+$)/','',$filteredFilename);
$filteredFilename = preg_replace('/-{2,}/','-',$filteredFilename);
$filteredFilenameLength = strlen($filteredFilename);
if ( $filteredFilenameLength > 80 ) {
if ($filteredFilename[80] === '-') {
echo 'case 1', PHP_EOL;
$filteredFilename = substr($filteredFilename, 0, 80);
} else {
echo 'case 2', PHP_EOL;
$filteredFilename = substr($filteredFilename, 0, strrpos($filteredFilename, '-', 80 - $filteredFilenameLength));
}
} else {
echo 'case 3', PHP_EOL;
}
echo $filteredFilenameLength, ' ', strlen($filteredFilename), ' ', $filteredFilename, PHP_EOL;
}