method encodeFilename documentation in bbn\Str
Returns a cross-platform filename for the file.
Example
X::dump(\bbn\Str::encodeFilename('test file/,1', 15, 'txt'));
// (string) "test_file_1.txt"
function($st, $maxlength = 50, $extension = NULL, $is_path = false)
{
$st = self::removeAccents(self::cast($st));
$allowed = '~\-_.\(\[\)\]';
// Arguments order doesn't matter
$args = \func_get_args();
foreach ($args as $i => $a){
if ($i > 0) {
if (\is_string($a)) {
$extension = $a;
}
elseif (\is_int($a)) {
$maxlength = $a;
}
elseif (\is_bool($a)) {
$is_path = $a;
}
}
}
if (!\is_int($maxlength)) {
$maxlength = mb_strlen($st);
}
if ($is_path) {
$allowed .= '/';
}
if ($extension
&& (self::fileExt($st) === self::changeCase($extension, 'lower'))
) {
$st = substr($st, 0, -(\strlen($extension) + 1));
}
elseif ($extension = self::fileExt($st)) {
$st = substr($st, 0, -(\strlen($extension) + 1));
}
// Replace non allowed character with single space
$st = mb_ereg_replace("([^\w\d".$allowed.".])", ' ', $st);
// Replace two or more spaces to one space
$st = mb_ereg_replace("\s{2,}", ' ', $st);
// Replace single spaces to under score
$st = mb_ereg_replace("\s", '_', $st);
// Remove the . character
$st = mb_ereg_replace("\.", '', $st);
;
$res = mb_substr($st, 0, $maxlength);
if ($extension) {
$res .= '.' . $extension;
}
return $res;
}
Returns a cross-platform filename for the file. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions