src/bbn/str.php
public static function encode_filename($st, $maxlength = 50, $extension = null, $is_path = false): string
  {

    $st = self::remove_accents(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;
        }
        else if ( \is_int($a) ){
          $maxlength = $a;
        }
        else if ( \is_bool($a) ){
          $is_path = $a;
        }
      }
    }

    if ( !\is_int($maxlength) ){
      $maxlength = mb_strlen($st);
    }

    if ( $is_path ){
      $allowed .= '/';
    }

    if (
      $extension &&
      (self::file_ext($st) === self::change_case($extension, 'lower'))
    ){
      $st = substr($st, 0, -(\strlen($extension)+1));
    }
    else if ( $extension = self::file_ext($st) ){
      $st = substr($st, 0, -(\strlen($extension)+1));
    }
    $st = mb_ereg_replace("([^\w\s\d".$allowed.".])", '', $st);
    $st = mb_ereg_replace("([\.]{2,})", '', $st);
    $res = mb_substr($st, 0, $maxlength);
    if ( $extension ){
      $res .= '.' . $extension;
    }
    return $res;
  }
public static function encode_filename($st, $maxlength = 50, $extension = null, $is_path = false): string
  {

    $st = self::remove_accents(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;
        }
        else if ( \is_int($a) ){
          $maxlength = $a;
        }
        else if ( \is_bool($a) ){
          $is_path = $a;
        }
      }
    }

    if ( !\is_int($maxlength) ){
      $maxlength = mb_strlen($st);
    }

    if ( $is_path ){
      $allowed .= '/';
    }

    if (
      $extension &&
      (self::file_ext($st) === self::change_case($extension, 'lower'))
    ){
      $st = substr($st, 0, -(\strlen($extension)+1));
    }
    else if ( $extension = self::file_ext($st) ){
      $st = substr($st, 0, -(\strlen($extension)+1));
    }
    $st = mb_ereg_replace("([^\w\s\d".$allowed.".])", '', $st);
    $st = mb_ereg_replace("([\.]{2,})", '', $st);
    $res = mb_substr($st, 0, $maxlength);
    if ( $extension ){
      $res .= '.' . $extension;
    }
    return $res;
  }
public static function encode_filename ( $ st , $ maxlength = 50 , $ extension = null , $ is_path = false ) : string { $ st = self :: remove_accents ( 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 ; } else if ( \ is_int ( $ a ) ) { $ maxlength = $ a ; } else if ( \ is_bool ( $ a ) ) { $ is_path = $ a ; } } } if ( ! \ is_int ( $ maxlength ) ) { $ maxlength = mb_strlen ( $ st ) ; } if ( $ is_path ) { $ allowed .= '/' ; } if ( $ extension && ( self :: file_ext ( $ st ) === self :: change_case ( $ extension , 'lower' ) ) ) { $ st = substr ( $ st , 0 , - ( \ strlen ( $ extension ) + 1 ) ) ; } else if ( $ extension = self :: file_ext ( $ st ) ) { $ st = substr ( $ st , 0 , - ( \ strlen ( $ extension ) + 1 ) ) ; } $ st = mb_ereg_replace ( "([^\w\s\d" . $ allowed . ".])" , '' , $ st ) ; $ st = mb_ereg_replace ( "([\.]{2,})" , '' , $ st ) ; $ res = mb_substr ( $ st , 0 , $ maxlength ) ; if ( $ extension ) { $ res .= '.' . $ extension ; } return $ res ; }
Returns a cross-platform filename for the file.

```php
\bbn\x::dump(\bbn\str::encode_filename('test file/,1', 15, 'txt'));
// (string) "test_file_1.txt"
```

@param string $st The name as string.
@param int $maxlength The maximum filename length (without extension), default: "50".
@param string $extension The extension of the file.
@param bool $is_path Tells if the slashes (/) are authorized in the string
@return string
Returns a cross - platform filename for the file .