elmaso
elmaso

Reputation: 193

PHP: Rawurlencode inside Preg-replace

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?

Upvotes: 0

Views: 340

Answers (2)

Alex Ciminian
Alex Ciminian

Reputation: 11508

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

Upvotes: 1

zaf
zaf

Reputation: 23244

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.

Upvotes: 1

Related Questions