Page 1 of 1
How do you set German characters lowercase, on the fly?
Posted: Tue May 03, 2016 11:10 am
by simonmlewis
Code: Select all
<?php
$uppercaseurlcheck = $_SERVER['REQUEST_URI'];
$converttolower = strtolower($uppercaseurlcheck);
$whatsmyurl = "http://" . $_SERVER['HTTP_HOST'];
if(preg_match('/[A-Z]/', $uppercaseurlcheck)){
header("Location: $whatsmyurl" . $converttolower,TRUE,301);
exit();
}
?>
We use this script to change any characters NOT in lowercase, to be lower. So the whole site is lower URLs.
Trouble is, for German characters, such as ö, it seems to think that is not lowercase in PHP, and then tries to set it, which causes POST forms to fail.
I can disable the lowercase function, but it means we have two urls for the same page, potentially.
So how can we do it, by only changing it if there is an uppercase letter? I did read you could do it with htaccess, but it required a conf change too which I am not comfortable with.
Re: How do you set German characters lowercase, on the fly?
Posted: Tue May 03, 2016 11:18 am
by Celauran
http://php.net/manual/en/function.mb-strtolower.php ?
[text]❯ php -a
Interactive shell
php > $string = 'vrööm vrøøm';
php > echo mb_strtolower($string);
vrööm vrøøm
php > $string_with_caps = 'VrÖöm vRøøm!';
php > echo mb_strtolower($string_with_caps);
vrööm vrøøm![/text]
Re: How do you set German characters lowercase, on the fly?
Posted: Tue May 03, 2016 11:25 am
by simonmlewis
Code: Select all
<?php
$uppercaseurlcheck = $_SERVER['REQUEST_URI'];
$converttolower = mb_strtolower($uppercaseurlcheck);
$whatsmyurl = "http://" . $_SERVER['HTTP_HOST'];
if(preg_match('/[A-Z]/', $uppercaseurlcheck)){
header("Location: $whatsmyurl" . $converttolower,TRUE,301);
exit();
}
?>
This doesn't work. It still sees that character as uppercase I guess, as it's redirecting it.
Actually, why it is seeing it as uppercase in the further place? preg_match.... should that see that German character as upper???
Re: How do you set German characters lowercase, on the fly?
Posted: Tue May 03, 2016 11:29 am
by Celauran
Do you need the preg_match? Couldn't you compare strings before and after being converted?
Re: How do you set German characters lowercase, on the fly?
Posted: Tue May 03, 2016 11:31 am
by simonmlewis
I could... how?
We want it to run as swiftly as poss. Idea is that it checks if it is uppercase, and then converts it. But I guess it's fractions of a sec to do what you are suggesting.
How tho?
Re: How do you set German characters lowercase, on the fly?
Posted: Tue May 03, 2016 11:35 am
by Celauran
simonmlewis wrote:I could... how?
It's just a string comparison.
Code: Select all
if ($uppercaseurlcheck != $converttolower) {
// redirect because they're different
}
Re: How do you set German characters lowercase, on the fly?
Posted: Tue May 03, 2016 3:50 pm
by requinix
The REQUEST_URI will contain the percent-encoded request. You need to decode that before you can lowercase it.
Re: How do you set German characters lowercase, on the fly?
Posted: Wed May 04, 2016 3:34 am
by simonmlewis
Bingo. This what you mean?
Code: Select all
<?php
$uppercaseurlcheck = $_SERVER['REQUEST_URI'];
$uppercaseurlcheck = rawurldecode($uppercaseurlcheck);
$converttolower = mb_strtolower($uppercaseurlcheck);
$whatsmyurl = "http://" . $_SERVER['HTTP_HOST'];
if(preg_match('/[A-Z]/', $uppercaseurlcheck)){
header("Location: $whatsmyurl" . $converttolower,TRUE,301);
exit();
}
?>