Конвертировать URL в Slug с PHP

0

Я использую приведенный ниже код, чтобы попытаться преобразовать его в slug, и по какой-то причине он не повторяет ничего. Я знаю, что мне не хватает чего-то чрезвычайно очевидного. Я не называю функцию?

<?php 

        $string = "Can't You Convert This To A Slug?";

        function clean($string) {
           $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
           return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
           echo $string;
        }

?>
  • 0
    Что такое пуля, и где URL, который вы включаете в пулю?
  • 1
    @Shahar в WordPress просторечия slug является URL key конкретной должности.
Показать ещё 1 комментарий
Теги:
replace

2 ответа

1
Лучший ответ

Вы эхом отзываетесь после выхода кода из функции.

попробуйте вот так:

 function clean_string($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
 }

$some = clean_string("Can't You Convert This To A Slug?");

echo $some;

Или вот так:

 function clean_me(&$string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
 }

$some = "Can't You Convert This To A Slug?";

clean_me($some);

echo $some;
1
<?php

        $string = "Can't You Convert This To A Slug?";

        function clean($string) {
           $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
           return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
        }

        $string = clean($string);
        echo $string;
?>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню