开发者

PHP: Rawurlencode inside Preg-replace

开发者 https://www.devze.com 2022-12-26 06:24 出处:网络
while($row = mysql_fetch_array($result)){ $search = \'\' . $row[\'searchquery\']; echo \'\' .开发者_开发知识库..

while($row = mysql_fetch_array($result)){ $search = '' . $row['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 +

is that possible?


The answer to your question would be to use a regular expression function that can actually handle UTF-8 strings.

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:

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 (ăşţ).

zaf is right, there are easier ways to do this :).


Do you have an objection to the str_replace function? If not then you can use it with rawurlencode. It would be a lot simpler and faster.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号