简体   繁体   English

PHP:Preg-replace内的Rawurlencode

[英]PHP: Rawurlencode inside Preg-replace

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

but if someone types in äöü, it doesnt show the letters - with rawurlencode thats possible, but I want to remove the blank spaces with pregreplace and replace it with + 但是如果有人输入äöü,它可能不会显示字母-可能是rawurlencode,但是我想用pregreplace删除空格,并用+代替

is that possible? 那可能吗?

Do you have an objection to the str_replace function? 您对str_replace函数有异议吗? If not then you can use it with rawurlencode. 如果没有,则可以将其与rawurlencode一起使用。 It would be a lot simpler and faster. 这将更加简单和快捷。

The answer to your question would be to use a regular expression function that can actually handle UTF-8 strings. 您问题的答案将是使用可以实际处理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) . '+/">';

You could also use the /u modifier for the preg functions: 您还可以对preg函数使用/u修饰符:

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

This seems better, but you should be careful. 这似乎更好,但是您应该小心。 I've tried it now, and it outputs your characters fine, but I noticed it stripped out some other UTF-8 characters I've given it as input (ăşţ). 我已经尝试过了,它可以很好地输出您的字符,但是我注意到它去除了其他一些我作为输入(UTS-8)输入的UTF-8字符。

zaf is right, there are easier ways to do this :). zaf是正确的,有更简单的方法可以做到这一点:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM