创建一个函数
function create_slug($string) {
$string = substr($string, 0, 100);
$string = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $string);
$string = strtolower(trim($string, '-'));
$string = preg_replace("/[\/_|+ -]+/", '-', $string);
return $string;
}
并像这样使用它
$actsAs = array(
'Sluggable.Sluggable' => array(
'label' => 'very & long & question',
'slug' => create_slug('very & long & question'),
'separator' => '-',
'overwrite' => false
)
);
print_r($actsAs);
// 输出
Array
(
[Sluggable.Sluggable] => Array
(
[label] => very & long & question
[slug] => very-long-question
[separator] => -
[overwrite] =>
)
)