-1

I get a city name as a GET variable. I need to make the first letter capitalized. If the Get variable is "herning" I kan with no problem make it H, but if the Variable is "ølstykke" I can only make it lowercase ø, not uppercase Ø.

header('Content-type: text/html; charset=utf-8');
print strToUpper(mb_substr($_GET["city"], 0, 1));

If I do not set the header to utf-8, I just get strange characters.

Any ideas?

Updated code

<?php
header('Content-type: text/html; charset=utf-8');
$city = mb_convert_case($_GET["city"], MB_CASE_TITLE, "UTF-8");//Ølstykke
print $city;
$section = file_get_contents('https://api.dataforsyningen.dk/steder?hovedtype=Bebyggelse&undertype=by&prim%C3%A6rtnavn='.$city);
$section = json_decode($section);
print '<pre>';
print_r($section);
print '</pre>';

Solution: urlencode() around $city when sending to dataforsyningen did the job.

Jeppe Donslund
  • 469
  • 9
  • 25
  • Does this answer your question? https://stackoverflow.com/questions/2517947/ucfirst-function-for-multibyte-character-encodings/2518021#2518021 and https://stackoverflow.com/questions/5969803/strtoupper-php-function-for-utf-8-string/5969837#5969837 – Syscall Apr 05 '21 at 17:48
  • https://www.php.net/manual/en/function.mb-strtoupper – Sammitch Apr 05 '21 at 19:04

1 Answers1

0

Use mb_strtoupper and specify the character-encoding in mb_substr

echo mb_strtoupper(mb_substr('ølstykke', 0, 1,'utf-8'));//Ø

In your case maybe you want not only first character but also the rest characters, so maybe mb_convert_case function can help you.

echo mb_convert_case('ølstykke', MB_CASE_TITLE, "UTF-8");//Ølstykke
XMehdi01
  • 5,538
  • 2
  • 10
  • 34
  • Yes that makes it uppercase. But then I need to send it via REST api to this address https://api.dataforsyningen.dk/steder?hovedtype=Bebyggelse&undertype=by&prim%C3%A6rtnavn=Ølstykke and that does not work. It is like the city name is wrong encoded. It works perfect with Herning or Vejle, but not with names containing æ, ø or å. – Jeppe Donslund Apr 05 '21 at 18:51
  • @JeppeDonslund then there is something else wrong with the code that you have yet to show us. – Sammitch Apr 05 '21 at 19:06
  • I have updated my question with the new code that works with herning, but not ølstykke or københavn – Jeppe Donslund Apr 05 '21 at 19:16
  • I found the trick. urlencode() when sending too Dataforsyningen. – Jeppe Donslund Apr 05 '21 at 19:59