public function slugify($slug)
{
// Remove HTML tags
$slug = preg_replace('/<(.*?)>/u', '', $slug);
// Remove inner-word punctuation.
$slug = preg_replace('/[\'"‘’“”\\[\\]\\(\\)\\{\\}:]/u', '', $slug);
if (craft()->config->get('allowUppercaseInSlug') === false) {
// Make it lowercase
$slug = StringHelper::toLowerCase($slug);
}
// Get the "words". Split on anything that is not a unicode letter or number. Periods, underscores, hyphens and forward slashes get a pass.
preg_match_all('/[\\p{L}\\p{N}\\.\\/_-]+/u', $slug, $words);
$words = ArrayHelper::filterEmptyStringsFromArray($words[0]);
$slug = implode(craft()->config->get('slugWordSeparator'), $words);
return $slug;
}
/** * @covers ::slugify */ public function testSlugifyShouldSlugifyString() { $string = 'Test string'; $slug = 'test-string'; $mockConfigService = $this->getMock('Craft\\ConfigService'); $mockConfigService->expects($this->any())->method('get')->willReturnCallback(function ($option) { if ($option == 'allowUppercaseInSlug') { return false; } elseif ($option == 'slugWordSeparator') { return '-'; } }); $this->setComponent(craft(), 'config', $mockConfigService); $service = new ImportService(); $result = $service->slugify($string); $this->assertSame($slug, $result); }