2011-01-21 122 views
1

我已經看到了這對被wordpress的完成,我沒有訪問Word新聞:)SEO友情鏈接,JS和/或php剝離

,但我需要返回一個URL字符串去除任何非有效的字符它並將一些字符轉換爲適當的字符:)

eg

1+ characters should be converted (of the following) 

[space]  = [dash] (1 dash) >>> (-) 
[underscore] = [dash] (1 dash) >>> (-) 
$str = 'Hello WORLD this is a bad string'; 
$str = convert_str_to_url($str); 
//output//NOTE: caps have are lowercase :) 
//hello-world-bad-string 

,並刪除常見的和毫無意義的詞語,如「在」 etccc「中」,「一」,

至少指向我的方向是正確的,如果ü天璣有GD代碼:)

回答

1

strtr可以用於此:

$replace = array(
    ' ' => '-', 
    '_' => '-', 
    'the' => '', 
    ... 
); 

$string = strtr($string, $replace); 
0

我將創建與str_replace函數()函數的函數。例如:

$str = 'Sentence with some words'; 
$str = strtolower($str); 

$searchNone = array('the', 'a', 'in'); 
$replaceNone = ''; 

$str = str_replace($searchNone, $replaceNone, $str); 

$search = array(chr(32)); //use ascii 
$replace = '-';  

$str = str_replace($search, $replace, $str); 

echo $str; 

使用以下網站的特殊字符:http://www.asciitable.com/

0

也許是這樣的:

function PrettyUri($theUri) 
{ 
    $aToBeReplace = array(' then ', ' the ', ' an ' 
    , ' a ', ' is ', ' are ', ' ', '_'); 
    $aReplacements = array(' ', ' ', ' ' 
    , ' ', ' ', ' ', '-', '-'); 
    return str_replace($aToBeReplace, $aReplacements, strtolower($theUri)); 
} 


echo PrettyUri('Hello WORLD this is a bad string');