问题
I've just tested locally my web application, everything works fine, but after uploading to server application behaves differently. I use function formatiraj_string_url to convert diacritic symbols and get clean url... locally it works fine but on server this function doesnt convert them the same way.
Few days earlier I tested this on some third server and it worked fine. Now I'm uploading web to test it again on this third server, but I just wonder what could really be the cause of such behavior?
function formatiraj_string_url($string)
{
$string = strtolower($string);
$znak[0] = ' ';
$znak[1] = 'Š';
$znak[2] = 'š';
$znak[3] = 'Đ';
$znak[4] = 'đ';
$znak[5] = 'Č';
$znak[6] = 'č';
$znak[7] = 'Ć';
$znak[8] = 'ć';
$znak[9] = 'Ž';
$znak[10] = 'ž';
$znak[11] = 'Š';
$znak[12] = 'Đ';
$znak[13] = 'Č';
$znak[14] = 'Ć';
$znak[15] = 'Ž';
$znak[16] = 'š';
$znak[17] = 'đ';
$znak[18] = 'č';
$znak[19] = 'ć';
$znak[20] = 'ž';
$znak[21] = 'Š'; // Š
$znak[22] = 'š'; // š
$zamjena[0] = '-';
$zamjena[1] = 's';
$zamjena[2] = 's';
$zamjena[3] = 'd';
$zamjena[4] = 'd';
$zamjena[5] = 'c';
$zamjena[6] = 'c';
$zamjena[7] = 'c';
$zamjena[8] = 'c';
$zamjena[9] = 'z';
$zamjena[10] = 'z';
$zamjena[11] = 's';
$zamjena[12] = 'd';
$zamjena[13] = 'c';
$zamjena[14] = 'c';
$zamjena[15] = 'z';
$zamjena[16] = 's';
$zamjena[17] = 'd';
$zamjena[18] = 'c';
$zamjena[19] = 'c';
$zamjena[20] = 'z';
$zamjena[21] = 's';
$zamjena[22] = 's';
$string = str_replace($znak, $zamjena, $string);
$new_string = preg_replace("/[^a-zA-Z0-9-s]/", "", $string);
return $new_string;
}
EDIT: Before str_replace, this function used preg_replace. On server this was the error showed:
Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 0 in /home2/sinjcom/public_html/sinj.com.hr/administracija/include/funkcije.php on line 200
But locally, I didn't have this problem
回答1:
Which charset is your file written in?
Since you have written the characters you would like to replace directly into your code, as strings, they are encoded in whatever charset the file uses. If that charset differs from what you get from the browser, your functions aren't going to work.
The important point you need to keep in mind, is to always keep track of the charset your strings are encoded with, and convert when neccessary.
Take a look at Kore Nordmanns FAQ about charsets in PHP for some more pointers about charsets.
回答2:
I recommend you rely on existing, highly-tested code to do this. I believe all these functions assume UTF-8 input and output 7-bit ASCII:
- remove_accents() from Wordpress
- Drupal's transliteration module (would require a little work to untie it from Drupal)
utf8_to_ascii()
from the venerable PHP UTF8 project- friendly_url() by Jakub Vrána
- Doctrine_Inflector::urlize()
- slugify() looks solid
来源:https://stackoverflow.com/questions/1556415/different-charset-on-different-server