Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP url encoding problems

I have some URL which is in CP-1251 i believe. For example:

http://domain.com/Test - суть в этом.mp3

mb_detect_encoding says it is ASCII. However, I've tried to convert this to UTF-8, but no luck. However the following worked:

$url = mb_convert_encoding(urldecode($url), "Windows-1251", "auto");

Which means that it converted the url to Windows-1251. Which is strange, but it shows the characters right. But when I insert this converted url inside an html object (some music player) it doesn't work. Firebug shows an error:

"NetworkError: 404 Not Found - http://domain.com/Test%20-%20????%20?%20????.mp3"

So somehow I got question marks instead of a right url. urlencode doesn't help.

The file itself is utf-8.

I'm confused with all this stuff. Is there any solution here?

like image 763
grjj3 Avatar asked Dec 28 '25 14:12

grjj3


2 Answers

Not exactly sure what answer you're looking for but its original encoding is Windows-1251, you can check with iconv:

var_dump(detect_encoding($url);

function detect_encoding($string) { 
  static $list = array('utf-8', 'windows-1251');

  foreach ($list as $item) {
    $sample = iconv($item, $item, $string);
    if (md5($sample) == md5($string))
      return $item;
  }
  return null;
}

This site can also be quite helpful: Universal Cyrillic Decoder

like image 63
Louis Avatar answered Dec 31 '25 17:12

Louis


Solved.

Just needed to take the file name separately and rawurlencode it.

like image 36
grjj3 Avatar answered Dec 31 '25 19:12

grjj3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!