ugrás a tartalomhoz

PHP - error utf-8

inf · 2012. Május. 24. (Cs), 17.28
Php-ben tákolok, próbálok mindent utf-8-ra beállítani. Az egyedüli, amivel gondjaim vannak, hogy az error.log-ba nem utf-8-ban mennek bele az adatok, így pl az átadott paramétereknél az ékezetes karakterek helyett csupa kérdőjel van. Mondjuk ez inkább szépséghiba, de kíváncsi vagyok, hogy lehetséges e orvosolni valahogy?
 
1

Nem kellene

janoszen · 2012. Május. 24. (Cs), 17.45
Az error logba mi írja az adatokat? Alapvetően nem kellene változnia semminek a logba íráskor, hacsak nincs közben valami elemző. Azt is vedd figyelembe, hogy annak a szoftvernek is UTF-8-ban kellene olvasnia az adatokat, amelyikkel nézed.
2

Hát netbeans elméletileg

inf · 2012. Május. 24. (Cs), 18.01
Hát netbeans elméletileg képes erre :D Az error.log-ba maga a php írja az adatokat, gondoltam valami ini beállítással lehetne konfigurálni, hogy ugyan már csinálja utf-8ban, de eddig nem találtam ilyet.
3

Milyen op.rendszeren? Nekem

eddig bírtam szó nélkül · 2012. Május. 24. (Cs), 18.39
Milyen op.rendszeren? Nekem valami ubuntu12 klónom van jelenleg (xubuntu), ezen az apache error_log-jába küldött UTF-8 kódolású, ékezetes karakterek így jelennek meg:

\xc3\x81rv\xc3\xadzt\xc5\xb1r\xc5\x91 T\xc3\xbck\xc3\xb6rf\xc3\xbar\xc3\xb3g\xc3\xa9p
(gyk: árvíztűrő tükörfúrógép :-))

Viszont az is tény, hogy ezen nem sikerült változtatnom az apache átkonfigurálásával. (feltéve, hogy jó helyen turkáltam)
4

Én win7 IIS -el tolom, nálam

inf · 2012. Május. 24. (Cs), 21.25
Én win7 IIS -el tolom, nálam kérdőjelek vannak, nem néztem meg, hogy melyik karaktert mi takar, nem érdekes... Közben kiderült, hogy a parse_url sem érti az utf-8-at :-) Vicces ez a php, elvileg netes nyelv, de megőszülsz, mire normálisan beállítod minden string függvényhez a karakterkódolást...
5

A vezérlőpultban állítsd át a

Karvaly84 · 2012. Május. 24. (Cs), 22.47
A vezérlőpultban állítsd át a alapértelmezett kódolást (mert win alat nem utf-8 a magyar nyelv esetén), szerintem az megoldja, bár windows-on még nem php-ztam még.
6

Valami ANSI bullshit az

inf · 2012. Május. 25. (P), 00.38
Valami ANSI bullshit az alapértelmezett, de annyira nem számít. Az utóbbi két napban kicsit elmerültem a php karakterkódolási hibái között. Egyelőre csak nagyjából átfutottam őket, nincsen rendesen letesztelve, de azért voltak vicces dolgok...

Amivel még gondban vagyok az az mb_split, mert pattern-t vár el, és nem stringet, emiatt, a split-tel és replace-el gondok vannak. Sajnos quote-olni sem lehet, mert nincs olyan mb függvény. Valszeg az lesz, hogy preg_split + qoute-al fogom valahogy megoldani mindkettőt. Elvileg be lehet configolni úgy a php-t, hogy az strtolower is működjön utf-8 szöveggel, viszont nekem valami olyasmi kéne, ami nem konfig függő...
  1. <?php  
  2.   
  3. if (!extension_loaded('mbstring') && !dl('mbstring'))  
  4.     throw new Exception('Extension mbstring required!');  
  5.   
  6. class Clean_String_Manipulator {  
  7.   
  8.     static protected $encoding = 'utf-8';  
  9.     static protected $regexFlag = 'u';  
  10.   
  11.     static public function isEmpty($string) {  
  12.         if (!is_string($string))  
  13.             throw new InvalidArgumentException('Subject must be a string.');  
  14.         return $string === '';  
  15.     }  
  16.   
  17.     static public function encodePattern($string) {  
  18.         return preg_quote($string);  
  19.     }  
  20.   
  21.     static public function encodeCamelCase($string$separator) {  
  22.         if (!is_string($string))  
  23.             throw new InvalidArgumentException('Subject must be a string.');  
  24.         if (!is_string($separator))  
  25.             throw new InvalidArgumentException('Separator must be a string.');  
  26.         try {  
  27.             $chunks = self::split(self::toLowerCase($string), $separator);  
  28.             for ($index = 1, $length = count($chunks); $index < $length; ++$index)  
  29.                 $chunks[$index] = self::upperCaseFirst($chunks[$index]);  
  30.             return self::concat($chunks'');  
  31.         } catch (Exception $e) {  
  32.             throw new Exception('String camelize failed: ' . $e);  
  33.         }  
  34.     }  
  35.   
  36.     static public function decodeCamelCase($string$separator) {  
  37.         if (!is_string($string))  
  38.             throw new InvalidArgumentException('Subject must be a string.');  
  39.         if (!is_string($separator))  
  40.             throw new InvalidArgumentException('Separator must be a string.');  
  41.         try {  
  42.             $escapedSeparator = self::replace($separator'$''\\$');  
  43.             $separated = self::replaceByPattern($string'%(?!^)\p{Lu}%usD'$escapedSeparator . '$0');  
  44.             return self::toLowerCase($separated);  
  45.         } catch (Exception $e) {  
  46.             throw new Exception('String deCamelize failed: ' . $e);  
  47.         }  
  48.     }  
  49.   
  50.     static public function encodeUrlComponents(array $components) {  
  51.         $url = '';  
  52.         if (isset($components['host']) && $components['host'] !== '') {  
  53.             if (isset($components['scheme']) && $components['scheme'] !== '')  
  54.                 $url.=$components['scheme'] . '://';  
  55.             $url.=$components['host'];  
  56.             if (isset($components['port']) && !in_array($components['port'], array('', 80, 8080)))  
  57.                 $url.=':' . $components['port'];  
  58.         }  
  59.         $url.='/';  
  60.         if (isset($components['path']) && $components['path'] !== '')  
  61.             $url.=$components['path'];  
  62.         if (isset($components['query']) && $components['query'] !== '')  
  63.             $url.='?' . $components['query'];  
  64.         if (isset($components['fragment']) && $components['fragment'] !== '')  
  65.             $url.='#' . $components['fragment'];  
  66.         return $url;  
  67.     }  
  68.   
  69.     static public function decodeUrlComponents($string) {  
  70.         if (!is_string($string))  
  71.             throw new InvalidArgumentException('Subject must be a string.');  
  72.         $escaped = self::replaceByPattern($string'%[^:/?#&=\.]+%sDe''urlencode(\'$0\')');  
  73.         $result = parse_url($escaped);  
  74.         if (!is_array($result))  
  75.             throw new Exception('String decodeUrlComponents failed.');  
  76.         foreach ($result as $key => $value)  
  77.             $result[$key] = urldecode($value);  
  78.         return $result;  
  79.     }  
  80.   
  81.     static public function encodeUrl($string) {  
  82.         if (!is_string($string))  
  83.             throw new InvalidArgumentException('Subject must be a string.');  
  84.         $result = urlencode($string);  
  85.         if (!is_string($result))  
  86.             throw new Exception('String urlEncode failed.');  
  87.         return $result;  
  88.     }  
  89.   
  90.     static public function decodeUrl($string) {  
  91.         if (!is_string($string))  
  92.             throw new InvalidArgumentException('Subject must be a string.');  
  93.         $result = urldecode($string);  
  94.         if (!is_string($result))  
  95.             throw new Exception('String urlEncode failed.');  
  96.         return $result;  
  97.     }  
  98.   
  99.     static public function encodeQuery(array $data) {  
  100.         if (!is_array($data))  
  101.             throw new InvalidArgumentException('Data must be an array.');  
  102.         return http_build_query($data);  
  103.     }  
  104.   
  105.     static public function decodeQuery($string) {  
  106.         if (!is_string($string))  
  107.             throw new InvalidArgumentException('Subject must be a string.');  
  108.         if (self::subString($string, 0, 1) == '?')  
  109.             $string = self::subString($string, 1, self::getLength($string));  
  110.         mb_parse_str($string$data);  
  111.         return $data;  
  112.     }  
  113.   
  114.     static public function subString($string$start$end) {  
  115.         if (!is_string($string))  
  116.             throw new InvalidArgumentException('Subject must be a string.');  
  117.         if (!is_int($start))  
  118.             throw new InvalidArgumentException('Start must be an integer.');  
  119.         if (!is_int($end))  
  120.             throw new InvalidArgumentException('End must be an integer.');  
  121.         if ($end < $start)  
  122.             throw new InvalidArgumentException('Start cannot be bigger than end.');  
  123.         $result = mb_substr($string$start$end - $start, self::$encoding);  
  124.         if (!is_string($result))  
  125.             throw new Exception('String toUpperCase failed.');  
  126.         return $result;  
  127.     }  
  128.   
  129.     static public function getLength($string) {  
  130.         if (!is_string($string))  
  131.             throw new InvalidArgumentException('Subject must be a string.');  
  132.         $result = mb_strlen($string, self::$encoding);  
  133.         if (!is_int($result))  
  134.             throw new Exception('String getLength failed.');  
  135.         return $result;  
  136.     }  
  137.   
  138.     static public function replace($string$search$replacement) {  
  139.         if (!is_string($string))  
  140.             throw new InvalidArgumentException('Subject must be a string.');  
  141.         if (!is_string($search))  
  142.             throw new InvalidArgumentException('Search must be a string.');  
  143.         if (!is_string($replacement))  
  144.             throw new InvalidArgumentException('Replacement must be a string.');  
  145.         $result = self::concat(self::split($string$search), $replacement);  
  146.         if (!is_string($result))  
  147.             throw new Exception('String replace failed.');  
  148.         return $result;  
  149.     }  
  150.   
  151.     static public function replaceByPattern($string$pattern$replacement) {  
  152.         if (!is_string($string))  
  153.             throw new InvalidArgumentException('Subject must be a string.');  
  154.         if (!is_string($pattern))  
  155.             throw new InvalidArgumentException('Pattern must be a string.');  
  156.         if (!is_string($replacement))  
  157.             throw new InvalidArgumentException('Replacement must be a string.');  
  158.         $pattern .= self::$regexFlag;  
  159.         $result = preg_replace($pattern$replacement$string);  
  160.         if (!is_string($result))  
  161.             throw new Exception('String replaceByPattern failed.');  
  162.         return $result;  
  163.     }  
  164.   
  165.     static public function matchFullByPattern($string$pattern) {  
  166.         if (!is_string($string))  
  167.             throw new InvalidArgumentException('Subject must be a string.');  
  168.         if (!is_string($pattern))  
  169.             throw new InvalidArgumentException('Pattern must be a string.');  
  170.         $pattern .= self::$regexFlag;  
  171.         preg_match($pattern$string$result);  
  172.         if (!isset($result) || !is_array($result))  
  173.             throw new Exception('String matchFullByPattern failed.');  
  174.         return $result;  
  175.     }  
  176.   
  177.     static public function matchByPattern($string$pattern) {  
  178.         try {  
  179.             $groups = self::matchFullByPattern($string$pattern);  
  180.             return $groups[0];  
  181.         } catch (Exception $e) {  
  182.             throw new Exception('String matchByPattern failed: ' . $e);  
  183.         }  
  184.     }  
  185.   
  186.     static public function matchGroupsByPattern($string$pattern) {  
  187.         try {  
  188.             $groups = self::matchFullByPattern($string$pattern);  
  189.             array_shift($groups);  
  190.             return $groups;  
  191.         } catch (Exception $e) {  
  192.             throw new Exception('String matchGroupsByPattern failed: ' . $e);  
  193.         }  
  194.     }  
  195.   
  196.     static public function testByPattern($string$pattern) {  
  197.         try {  
  198.             $groups = self::matchFullByPattern($string$pattern);  
  199.             return emptyempty($groups);  
  200.         } catch (Exception $e) {  
  201.             throw new Exception('String testByPattern failed: ' . $e);  
  202.         }  
  203.     }  
  204.   
  205.     static public function split($string$separator) {  
  206.         if (!is_string($string))  
  207.             throw new InvalidArgumentException('Subject must be a string.');  
  208.         if (!is_string($separator))  
  209.             throw new InvalidArgumentException('Separator must be a string.');  
  210.         $result = mb_split($separator$string);  
  211.         if (!is_array($result))  
  212.             throw new Exception('String split failed.');  
  213.         return $result;  
  214.     }  
  215.   
  216.     static public function splitByPattern($string$pattern) {  
  217.         if (!is_string($string))  
  218.             throw new InvalidArgumentException('Subject must be a string.');  
  219.         if (!is_string($pattern))  
  220.             throw new InvalidArgumentException('Pattern must be a string.');  
  221.         $pattern .= self::$regexFlag;  
  222.         $result = preg_split($pattern$string);  
  223.         if (!is_array($result))  
  224.             throw new Exception('String splitByPattern failed.');  
  225.         return $result;  
  226.     }  
  227.   
  228.     static public function splitByPatternGroups($string$pattern) {  
  229.         if (!is_string($string))  
  230.             throw new InvalidArgumentException('Subject must be a string.');  
  231.         if (!is_string($pattern))  
  232.             throw new InvalidArgumentException('Pattern must be a string.');  
  233.         $pattern .= self::$regexFlag;  
  234.         if ($keepCapturingGroups)  
  235.             $flagCode = PREG_SPLIT_DELIM_CAPTURE;  
  236.         else  
  237.             $flagCode = 0;  
  238.         $result = preg_split($pattern$string, -1, PREG_SPLIT_DELIM_CAPTURE);  
  239.         if (!is_array($result))  
  240.             throw new Exception('String splitByPattern failed.');  
  241.         return $result;  
  242.     }  
  243.   
  244.     static public function concat(array $chunks$separator) {  
  245.         if (!is_string($separator))  
  246.             throw new InvalidArgumentException('Separator must be a string.');  
  247.         $result = implode($separator$chunks);  
  248.         if (!is_string($result))  
  249.             throw new Exception('String concat failed.');  
  250.         return $result;  
  251.     }  
  252.   
  253.     static public function toLowerCase($string) {  
  254.         if (!is_string($string))  
  255.             throw new InvalidArgumentException('Subject must be a string.');  
  256.         $result = mb_strtolower($string, self::$encoding);  
  257.         if (!is_string($result))  
  258.             throw new Exception('String toLowerCase failed.');  
  259.         return $result;  
  260.     }  
  261.   
  262.     static public function toUpperCase($string) {  
  263.         if (!is_string($string))  
  264.             throw new InvalidArgumentException('Subject must be a string.');  
  265.         $result = mb_strtoupper($string, self::$encoding);  
  266.         if (!is_string($result))  
  267.             throw new Exception('String toUpperCase failed.');  
  268.         return $result;  
  269.     }  
  270.   
  271.     static public function lowerCaseWords($string) {  
  272.         if (!is_string($string))  
  273.             throw new InvalidArgumentException('Subject must be a string.');  
  274.         try {  
  275.             $chunks = self::splitByPattern($string'%(\s+)%sD', true);  
  276.             foreach ($chunks as $index => $chunk)  
  277.                 $chunks[$index] = self::lowerCaseFirst($chunk);  
  278.             return self::concat($chunks'');  
  279.         } catch (Exception $e) {  
  280.             throw new Exception('String lowerCaseWords failed: ' . $e);  
  281.         }  
  282.     }  
  283.   
  284.     static public function upperCaseWords($string) {  
  285.         if (!is_string($string))  
  286.             throw new InvalidArgumentException('Subject must be a string.');  
  287.         try {  
  288.             $chunks = self::splitByPattern($string'%(\s+)%sD', true);  
  289.             foreach ($chunks as $index => $chunk)  
  290.                 $chunks[$index] = self::upperCaseFirst($chunk);  
  291.             return self::concat($chunks'');  
  292.         } catch (Exception $e) {  
  293.             throw new Exception('String upperCaseWords failed: ' . $e);  
  294.         }  
  295.     }  
  296.   
  297.     static public function lowerCaseFirst($string) {  
  298.         if (!is_string($string))  
  299.             throw new InvalidArgumentException('Subject must be a string.');  
  300.         try {  
  301.             return self::toLowerCase(self::subString($string, 0, 1)) . self::subString($string, 1, self::getLength($string));  
  302.         } catch (Exception $e) {  
  303.             throw new Exception('String lowerCaseFirst failed: ' . $e);  
  304.         }  
  305.     }  
  306.   
  307.     static public function upperCaseFirst($string) {  
  308.         if (!is_string($string))  
  309.             throw new InvalidArgumentException('Subject must be a string.');  
  310.         try {  
  311.             return self::toUpperCase(self::subString($string, 0, 1)) . self::subString($string, 1, self::getLength($string));  
  312.         } catch (Exception $e) {  
  313.             throw new Exception('String upperCaseFirst failed: ' . $e);  
  314.         }  
  315.     }  
  316.   
  317. }  
7

PCRE

Poetro · 2012. Május. 25. (P), 06.52
A PCRE függvények támogatnak UTF-8-at, csak meg kell nekik mondani az u módosítóval.
8

Tisztában vagyok vele, azokat

inf · 2012. Május. 25. (P), 12.12
Azokat használom, az u módosítót meg automatikusan csapja hozzá a rendszer, így nem tudom elfelejteni... Azt hiszem a teljes %%flag részt is automatikusan fogom hozzáírni, mert mindig usD-t használok, nagyon ritka esetben meg e-t is.
(szerk: Azt hiszem ez nem megvalósítható, nem szereti, ha a % szerepel a mintában. A sima szöveget is csak úgy tudtam betrükközni, hogy preg_quote és a határolónak meg +-t választottam.)

Azt hiszem ezek egy része benne van a PEAR i18n csomagokban.