PHP:Preg-replace中的Rawurlencode

时间:2010-04-05 18:05:03

标签: php preg-replace

while($ row = mysql_fetch_array($ result)){     $ search =''。 $行[ 'SEARCHQUERY'];
    echo''...

但是如果有人输入äöü,它不会显示字母 - 使用rawurlencode可能,但我想用pregreplace删除空格并用+替换它

可能吗?

2 个答案:

答案 0 :(得分:1)

您对str_replace函数有异议吗?如果没有,那么你可以将它与rawurlencode一起使用。它会更简单,更快。

答案 1 :(得分: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是对的,有更简单的方法:)。