views:

121

answers:

2

Hello, I have this snippet

while($row = mysql_fetch_array($result)){
$search = '' . $row['searchquery'];  
echo '<a href="http://www.example.com/' . preg_replace( array('/[^\s\w]/','/\s/'),array('','+'),$search) . '+/">' ...

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?

+1  A: 

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.

zaf
+1  A: 

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 :).

Alex Ciminian
Dude, click that up arrow :P
zaf