2014-02-23 92 views
0

我正在使用下面的代码尝试转换为slug,并且由于某种原因它没有回显任何内容。我知道我错过了一件非常明显的事情。我是不是在调用这个函数?使用PHP将链接转换为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

什么是蛞蝓和地方就是你覆盖成栓的网址是什么? – Shahar

+1

@Shahar用'WordPress'的说法是'slug'是特定帖子的'URL键'。 –

+0

你永远不会调用该方法...在底部添加'clean($ string);'。 – Shahar

回答

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; 
?>