본문 바로가기
css

게시 제목에서 URL 슬러그 만들기

by code-box 2021. 10. 9.
반응형

단어 사이의 공백을 하이픈으로 바꾸는 정규식 함수입니다.

<?php
function create_slug($string){
     $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
     return $slug;
}
echo create_slug('does this thing work or not');
//returns 'does-this-thing-work-or-not'
?>

댓글