Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将URL转换为Slug_Php_Jquery_Replace - Fatal编程技术网

使用PHP将URL转换为Slug

使用PHP将URL转换为Slug,php,jquery,replace,Php,Jquery,Replace,我正在使用下面的代码尝试转换为slug,但由于某些原因,它没有响应任何内容。我知道我遗漏了一些非常明显的东西。我不是在调用函数吗 <?php $string = "Can't You Convert This To A Slug?"; function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyp

我正在使用下面的代码尝试转换为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;
或者像这样:

 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;

什么是slug?你在slug中覆盖的URL在哪里?@Shahar在
WordPress
中的说法
slug
是特定帖子的
URL键。你永远不会调用该方法。。。添加
clean($string)在底部。