Как заменить «:» на «/» в функции slugify?

У меня есть функция, которая подталкивает текст, он работает хорошо, за исключением того, что мне нужно заменить «:» на «/». В настоящее время он заменяет все небуквенные символы или цифры «-». Вот :

function slugify($text) { // replace non letter or digits by - $text = preg_replace('~[^\\pL\d]+~u', '-', $text); // trim $text = trim($text, '-'); // transliterate if (function_exists('iconv')) { $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); } // lowercase $text = strtolower($text); // remove unwanted characters $text = preg_replace('~[^-\w]+~', '', $text); if (empty($text)) { return 'n-a'; } return $text; } 

Я сделал пару модификаций. Я предоставил набор для поиска / замены массивов, чтобы мы заменили большинство всего на - , но заменим : на / :

 $search = array( '~[^\\pL\d:]+~u', '~:~' ); $replace = array( '-', '/' ); $text = preg_replace( $search, $replace, $text); 

И позже, это последнее preg_replace нашу / пустой строкой. Поэтому я допустил удары с червями в классе символов.

 $text = preg_replace('~[^-\w\/]+~', '', $text); 

Что выводит следующее:

 // antiques/antiquities echo slugify( "Antiques:Antiquities" ); 

Вы можете проверить этот код:

 // php tag start Class MyClass { public function slug($str, $char ) { // Lower case the string and remove whitespace from the beginning or end $str = trim(strtolower($str)); // Remove single quotes from the string $str = str_replace(“'”, ”, $str); // Every character other than az, 0-9 will be replaced with a single dash (-) $str = preg_replace(“/[^a-z0-9]+/”, $char, $str); // Remove any beginning or trailing dashes $str = trim($str, $char); return $str; } } //Creating object of the MyClass $obj = new MyClass(); $str = "What is your name? "; echo $obj->getSlug($str , '-'); // php tag end 

Перейдите по следующей ссылке для четкого понимания

Как удалить строку / предложение в php?