Phalcon\Utils\Slug::generate PHP Метод

generate() публичный статический Метод

Creates a slug to be used for pretty URLs.
public static generate ( string $string, array $replace = [], string $delimiter = '-' ) : string
$string string
$replace array
$delimiter string
Результат string
    public static function generate($string, $replace = [], $delimiter = '-')
    {
        if (!extension_loaded('intl')) {
            throw new Exception('intl module not loaded');
        }
        // Save the old locale and set the new locale to UTF-8
        $oldLocale = setlocale(LC_ALL, '0');
        setlocale(LC_ALL, 'en_US.UTF-8');
        // Better to replace given $replace array as index => value
        // Example $replace['ı' => 'i', 'İ' => 'i'];
        if (!empty($replace) && is_array($replace)) {
            $string = str_replace(array_keys($replace), array_values($replace), $string);
        }
        $transliterator = \Transliterator::create('Any-Latin; Latin-ASCII');
        $string = $transliterator->transliterate(mb_convert_encoding(htmlspecialchars_decode($string), 'UTF-8', 'auto'));
        // replace non letter or non digits by -
        $string = preg_replace('#[^\\pL\\d]+#u', '-', $string);
        // Trim trailing -
        $string = trim($string, '-');
        $clean = preg_replace('~[^-\\w]+~', '', $string);
        $clean = strtolower($clean);
        $clean = preg_replace('#[\\/_|+ -]+#', $delimiter, $clean);
        $clean = trim($clean, $delimiter);
        // Revert back to the old locale
        setlocale(LC_ALL, $oldLocale);
        return $clean;
    }

Usage Example

Пример #1
0
 public function testCreateDocument()
 {
     $data = ['title' => 'Title test', 'content' => 'Content test', 'category_id' => new \MongoId()];
     $fake = new Fake();
     $this->assertInstanceOf('\\Vegas\\Db\\Decorator\\CollectionAbstract', $fake);
     $fake->writeAttributes($data);
     foreach ($data as $key => $value) {
         $this->assertEquals($value, $fake->readAttribute($key));
     }
     $fake->generateSlug($data['title']);
     $slug = new Slug();
     $this->assertEquals($slug->generate($data['title']), $fake->readAttribute('slug'));
     $this->assertEquals($data['title'], $fake->readMapped('title'));
     $this->assertEquals(json_encode($fake->toArray()), json_encode($fake->toMappedArray()));
     $this->assertTrue($fake->save());
 }
All Usage Examples Of Phalcon\Utils\Slug::generate