Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]problemy z mb_detect_encoding
Forum PHP.pl > Forum > Przedszkole
Larges
Witam, napotkałem następujący problem.

Przykład następujący działa, ukazuje mi "UTF-8".

Kod
$content = "źóóóńćąś";
echo mb_detect_encoding($content);


Docelowo chcę pobrać tekst z pliku którego kodowania niestety nie znam - jakiś windows (1252 lub inne wersje). Pobieram treść z plików a wiele z nich jest w różnych kodowaniach więc potrzebuję skrypt ustalający kodowanie pliku z zewnętrznego serwera.

Kod
$content = file_get_contents('http://domena.pl/common/sart/' . $id_operatora . '/' . $id_operatora . '_' . $id_oferty . '_atrakcje.txt');
echo mb_detect_encoding($content);


Niestety powyższy przykład nie działa (;

Mógłbym prosić o jakąś wskazówkę, co w takim przypadku czynić? - najlepiej z linkiem do przykładu.
pawel.b
w jakim sensie nie działa. Co się pojawia na ekranie questionmark.gif
Larges
Nic się nie pojawia właśnie ;].
pawel.b
podejrzewam że nic nie pobierasz z urla
odpal to :
  1. <?php
  2. $content = file_get_contents('http://domena.pl/common/sart/' . $id_operatora . '/' . $id_operatora . '_' . $id_oferty . '_atrakcje.txt');
  3. var_dump($content);
  4. $encoding = mb_detect_encoding($content);
  5. var_dump($encoding);
  6. ?>
Larges
Kod
string(844) "��W� �o�b�i�e�k�c�i�e� �s�z�k�o�Ba� �j�e�zd�z�i�e�c�k�a� �d�l�a� �p�o�c�z�t�k�u�j�c�y�c�h� �i� �z�a�a�w�a�n�s�o�w�a�n�y�c�h�,� �s�p�a�c�e�r�y� �k�o�n�n�e�,� �j�a�z�d�a� �b�r�y�c�z�k� �l�u�b� �n�a� �s�a�n�i�a�c�h�,� � �k�o�r�t�y� �t�e�n�i�s�o�w�e�,� �p�l�a�c� �z�a�b�a�w� �d�l�a� �d�z�i�e�c�i�,� �w�d�k�a�r�s�t�w�o�,� �w�Ba�s�n�a� �p�r�z�y�s�t�a�D �d�l�a� �Bo�d�z�i� �i� �j�a�c�h�t���w�,� �s�a�u�n�a� �i� �b�i�l�a�r�d�.� � � � �<�B�>�Z�N�I�{K�A� �D�L�A� �S�T�A�AY�C�H� �K�L�I�E�N�T���W� �!� � �O�s�o�b�y� �k�o�r�z�y�s�t�a�j�c�e� �p�r�z�e�z� �t�r�z�y� �k�o�l�e�j�n�e� �l�a�t�a� �z� �n�a�s�z�e�j� �o�f�e�r�t�y� �z�n�i�|k�a� �5�0� �z�B �<�/�B�>�U�w�a�g�i�!� � � �1�.�z�n�i�|k�a� � �o�b�o�w�i�z�u�j�e� �p�r�z�e�z� �c�a�By� �s�e�z�o�n�,� � �2�.�z�n�i�|k�a� �n�i�e� �Bc�z�y� �s�i� �z� �i�n�n�y�m�i� �p�r�o�m�o�c�j�a�m�i�.�" bool(false)


(;
pawel.b
ok
to teraz tak
  1. <?php
  2. $content = file_get_contents('http://domena.pl/common/sart/' . $id_operatora . '/' . $id_operatora . '_' . $id_oferty . '_atrakcje.txt');
  3. for($i = 0;$i<mb_strlen($content);$i++)
  4. {
  5. var_dump(mb_detect_encoding($content{$i}))."<br/>";
  6. }
  7. ?>

zobaczysz z jakimi znakami masz probelem
Larges
ok wynik kolejnego testu - troszkę spory, nie lubię zaśmiecać ale wkleję przynajmniej część.

Kod
bool(false) bool(false) string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "UTF-8" string(5) "UTF-8" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII"



Czy sądzić zatem że plik ma różne kodowania? "ASCII", "UTF-8" ?

Ogólnie dzięki za pomoc i cierpliwość (;
pawel.b
Plik masz w utf -8 tam gdzie jest asci to pewnie jest zwykła litera jednobajtowa która w każdym kodowaniu wygląda tak samo. Natomiast na początku masz false 3 razy to znaczy że pewnie masz tam boma źle zapisanego. Moja propozycja to wycinać te znaki dla których detect_encoding zwraca false.

czyli:
  1. <?php
  2. $content = file_get_contents('http://domena.pl/common/sart/' . $id_operatora . '/' . $id_operatora . '_' . $id_oferty . '_atrakcje.txt');
  3. for($i = 0;$i<mb_strlen($content);$i++)
  4. {
  5. if(mb_detect_encoding($content{$i}))
  6. {
  7. $new_content +=$content{$i};
  8. }
  9. }
  10.  
  11. echo mb_detect_encoding($new_content);
  12. ?>
Larges
Powyższy przykład generuje niemalże identyczny kod jak poprzedni. Również mam bool(false) bool(false).

Kod
bool(false) bool(false) string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "UTF-8" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "UTF-8" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5) "ASCII" string(5)
pawel.b
niemożliwe smile.gif
nigdzie nie ma var_dumpa i jest tylko jedno echo
Larges
ale Wtopa. Masz rację. Wynik to: ASCII.

Dzięki za pomoc.
pawel.b
spoko ale według mnie wynik powinien być UTF-8
coś jest chyba nie tak
spróbuj jeszcze tak smile.gif
  1. <?php
  2. $content = file_get_contents('http://domena.pl/common/sart/' . $id_operatora . '/' . $id_operatora . '_' . $id_oferty . '_atrakcje.txt');
  3. for($i = 0;$i<mb_strlen($content);$i++)
  4. {
  5. if(mb_detect_encoding(mb_substr($content,$i,1)))
  6. {
  7. $new_content +=mb_substr($content,$i,1);
  8. }
  9. }
  10.  
  11. echo mb_detect_encoding($new_content);
  12. ?>
Larges
mimo szczerych chęci ASCII (;
pawel.b
na początku daj
mb_internal_encoding('UTF-8');
Larges
Chodzi o następujące rozwiązanie ?

Kod
mb_internal_encoding('UTF-8');
$content = file_get_contents('http://www.domena.pl/common/sart/' . $id_operatora . '/' . $id_operatora . '_' . $id_oferty . '_atrakcje.txt');
for($i = 0;$i<mb_strlen($content);$i++)
{
if(mb_detect_encoding(mb_substr($content,$i,1)))
{
$new_content +=mb_substr($content,$i,1);
}
}

echo mb_detect_encoding($new_content);




- ASCII
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.