Bolt\Helpers\Str::makeSafe PHP Method

makeSafe() public static method

Returns a "safe" version of the given string - basically only US-ASCII and numbers. Needed because filenames and titles and such, can't use all characters.
public static makeSafe ( string $str, boolean $strict = false, string $extrachars = '' ) : string
$str string
$strict boolean
$extrachars string
return string
    public static function makeSafe($str, $strict = false, $extrachars = '')
    {
        $str = str_replace('&', '', $str);
        $delim = '/';
        if ($extrachars != '') {
            $extrachars = preg_quote($extrachars, $delim);
        }
        if ($strict) {
            $slugify = Slugify::create(['regexp' => '/[^a-z0-9_' . $extrachars . ' -]+/']);
            $str = $slugify->slugify($str, '');
            $str = str_replace(' ', '-', $str);
        } else {
            // Allow Uppercase and don't convert spaces to dashes
            $slugify = Slugify::create(['regexp' => '/[^a-zA-Z0-9_.,' . $extrachars . ' -]+/', 'lowercase' => false]);
            $str = $slugify->slugify($str, '');
        }
        return $str;
    }

Usage Example

 public function testMakeSafe()
 {
     // basic
     $input = "this is a ƃuıɹʇs ʇsǝʇ";
     $this->assertEquals("this is a uis s", Str::makeSafe($input));
     //strict
     $input = ";this is a ƃuıɹʇs ʇsǝʇ";
     $this->assertEquals("this-is-a-uis-s", Str::makeSafe($input, true));
     // extra chars
     $input = ";this is a ƃuıɹʇs ʇsǝʇ";
     $this->assertEquals(";this-is-a-uis-s", Str::makeSafe($input, true, ';'));
     // German
     $input = "Ich lässt öfter mit Vätern in einer kleinen Straße";
     $this->assertEquals("ich-laesst-oefter-mit-vaetern-in-einer-kleinen-strasse", Str::makeSafe($input, true, ';'));
     // French
     $input = "Et le proxénète s'est avancé à la barre";
     $this->assertEquals("et-le-proxenete-sest-avance-a-la-barre", Str::makeSafe($input, true, ';'));
     // Swedish
     $input = "Skämt åsido satan vilket uruselt tillvägagångsätt";
     $this->assertEquals("skaemt-aasido-satan-vilket-uruselt-tillvaegagaangsaett", Str::makeSafe($input, true, ';'));
 }
All Usage Examples Of Bolt\Helpers\Str::makeSafe