2010-04-05 61 views
0

while($ row = mysql_fetch_array($ result)){ $ search =''。 $行[ 'SEARCHQUERY'];
回聲「」 ...PHP:Preur-replace中的Rawurlencode

但如果有人AOU類型,它不顯示字母 - 用rawurlencode那是可能的,但我想與pregreplace刪除空格和替換它與+

是那可能嗎?

回答

1

您的問題的答案是使用正則表達式函數,它可以實際處理UTF-8字符串。

mb_internal_encoding("UTF-8"); 
mb_regex_encoding('UTF-8'); 

// eliminate the first spaces 
$search = mb_ereg_replace('^\s*', '', ''.$row['searchquery']); 
// replace the other spaces with '+' signs 
echo '<a href="http://www.example.com/' . mb_ereg_replace('\s', '+', $search) . '+/">'; 

您也可以使用/u修改爲preg功能:

echo '<a href="http://www.example.com/' . preg_replace(array('/[^\s\w]/u','/\s/u'),array('','+'),$search) . '+/">' 

這似乎更好,但你要小心。我現在已經嘗試過了,它輸出的字符很好,但是我注意到它除去了一些其他的UTF-8字符,我把它作爲輸入(ăşţ)。

zaf是對的,有更簡單的方法來做到這一點:)。

+1

夥計,點擊向上箭頭:P – zaf 2010-04-05 18:52:31

1

你有反對str_replace函數嗎?如果沒有,那麼你可以使用rawurlencode。這會更簡單快捷。

相關問題