好吧,如果我理解正確的,你想去掉一切但自然字符;如果是這樣的話,那麼你可以這樣做:(代碼有很好的註釋:))
<?php
echo '<pre>';
$names = array(
'polar bear',
'Grevy\'s zebra',
'wet \'n wild!',
'Nature: Alaska',
'#myName',
'dog % cat',
);
echo "names:\n";
print_r($names);
// "urlencode"ing them!
$names_encoded = array();
foreach($names as $name)
$names_encoded[] = urlencode($name);
echo "\n\nnames_encoded:\n";
print_r($names_encoded);
$out = array();
foreach($names_encoded as $name_encoded)
$out[] = preg_replace(
array('#%[0-9a-fA-F]{2}#', '#\+#'), // finds the patterns like '%HH' (H represents an Hexadecimal digit), and '+'; the latter representing the spaces
array('', '-'), // replace the '%HH' matches with nothing ('') and the '+' matches with '-'
$name_encoded
);
echo "\n\nout:\n";
print_r($out);
?>
輸出的摘錄是:
out:
Array
(
[0] => polar-bear
[1] => Grevys-zebra
[2] => wet-n-wild
[3] => Nature-Alaska
[4] => myName
[5] => dog--cat
)
您不必刪除的話,只是編碼\逃脫他們 –
什麼'mysite的/生活/細紋%-27-zebra'? – chris85
Dagon,如果我理解正確,編碼意味着URL看起來像grevy's-zebra。如果我想要一個像grevys-zebra這樣的「乾淨的URL」,我需要轉義’。我怎麼做? –