Я использую приведенный ниже код, чтобы попытаться преобразовать его в 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;
}
?>
Вы эхом отзываетесь после выхода кода из функции.
попробуйте вот так:
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;
<?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;
?>
WordPress
просторечияslug
являетсяURL key
конкретной должности.