How do you set German characters lowercase, on the fly?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you set German characters lowercase, on the fly?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you set German characters lowercase, on the fly?

Post 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]
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you set German characters lowercase, on the fly?

Post 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???
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you set German characters lowercase, on the fly?

Post by Celauran »

Do you need the preg_match? Couldn't you compare strings before and after being converted?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you set German characters lowercase, on the fly?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you set German characters lowercase, on the fly?

Post by Celauran »

simonmlewis wrote:I could... how?
It's just a string comparison.

Code: Select all

if ($uppercaseurlcheck != $converttolower) {
    // redirect because they're different
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you set German characters lowercase, on the fly?

Post by requinix »

The REQUEST_URI will contain the percent-encoded request. You need to decode that before you can lowercase it.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you set German characters lowercase, on the fly?

Post 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();
}
?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply