torrentz Posted March 6, 2009 Author Report Share Posted March 6, 2009 (edited) Gadījās problēma. PHP tā īsti nedraudzējas ar UTF-8: Ja, vārds ir ar kirilicas burtiem, piemēram: барбамил, tad šis kods $ran_1 = rand(0, $cik); $ran_2 = rand(0, $cik); $ran_3 = rand(0, $cik); $ran_4 = rand(0, $cik); $ran_5 = rand(0, $cik); $ran_6 = rand(0, $cik); $ran_7 = rand(0, $cik); $ran_8 = rand(0, $cik); $ran_9 = rand(0, $cik); $vards = str_replace( $atb[$ran_1], "-", $ottt ); $vards = str_replace($vards[$ran_2], "-", $vards); $vards = str_replace($vards[$ran_3], "-", $vards); $vards = str_replace($vards[$ran_4], "-", $vards); if($cik >= 5) $vards = str_replace($vards[$ran_5], "-", $vards); if($cik >= 6) $vards = str_replace($vards[$ran_6], "-", $vards); if($cik >= 7) $vards = str_replace($vards[$ran_7], "-", $vards); if($cik >= 8) $vards = str_replace($vards[$ran_8], "-", $vards); if($cik >= 9) $vards = str_replace($vards[$ran_9], "-", $vards); echo $vards; izvadās kā ----�------�---- Laikm PHP nemāk sadalīt vārdu kā pseidomasīvu (vārdu sadalīt pa burtiem), ja tas ir multibaitīgais vārds... Ir kādas idejas? Edited March 6, 2009 by torrentz Quote Link to comment Share on other sites More sharing options...
bubu Posted March 6, 2009 Report Share Posted March 6, 2009 Multibyte stringiem jālieto mb_xxx saimes funkcijas, tās darbosies ar utf-8 korekti. Tavā gadījumā $vards[$ran_X] vajag aizstāt ar mb_substr($vards, $ran_X, 1); Quote Link to comment Share on other sites More sharing options...
torrentz Posted March 7, 2009 Author Report Share Posted March 7, 2009 "вицин" vēljoprojām pārvēršas par: "----�-----" $cik = mb_strlen($ottt)-1; $ran_1 = rand(0, $cik); $ran_2 = rand(0, $cik); $ran_3 = rand(0, $cik); $ran_4 = rand(0, $cik); $ran_5 = rand(0, $cik); $ran_6 = rand(0, $cik); $ran_7 = rand(0, $cik); $ran_8 = rand(0, $cik); $ran_9 = rand(0, $cik); $vards = str_replace(mb_substr($ottt, $ran_1, 1), "-", $ottt ); $vards = str_replace(mb_substr($vards, $ran_2, 1), "-", $vards); $vards = str_replace(mb_substr($vards, $ran_3, 1), "-", $vards); $vards = str_replace(mb_substr($vards, $ran_4, 1), "-", $vards); if($cik >= 5) $vards = str_replace(mb_substr($vards, $ran_5, 1), "-", $vards); if($cik >= 6) $vards = str_replace(mb_substr($vards, $ran_6, 1), "-", $vards); if($cik >= 7) $vards = str_replace(mb_substr($vards, $ran_7, 1), "-", $vards); if($cik >= 8) $vards = str_replace(mb_substr($vards, $ran_8, 1), "-", $vards); if($cik >= 9) $vards = str_replace(mb_substr($vards, $ran_9, 1), "-", $vards); Quote Link to comment Share on other sites More sharing options...
torrentz Posted March 7, 2009 Author Report Share Posted March 7, 2009 Ārprāc, es laikam nojūgšos- pat šis nestrādā: $ran_1 = rand(0, $cik); $ran_2 = rand(0, $cik); $ran_3 = rand(0, $cik); $ran_4 = rand(0, $cik); $ran_5 = rand(0, $cik); $ran_6 = rand(0, $cik); $ran_7 = rand(0, $cik); $ran_8 = rand(0, $cik); $ran_9 = rand(0, $cik); function mb_str_replace($haystack, $search,$replace, $offset=0,$encoding='auto'){ $len_sch=mb_strlen($search,$encoding); $len_rep=mb_strlen($replace,$encoding); while (($offset=mb_strpos($haystack,$search,$offset,$encoding))!==false){ $haystack=mb_substr($haystack,0,$offset,$encoding) .$replace .mb_substr($haystack,$offset+$len_sch,strlen($haystack),$encoding); $offset=$offset+$len_rep; if ($offset>mb_strlen($haystack,$encoding))break; } return $haystack; } $vards = mb_str_replace($ottt, mb_substr($ottt, $ran_1, 1), "-"); $vards = mb_str_replace($vards, mb_substr($vards, $ran_2, 1), "-"); $vards = mb_str_replace($vards, mb_substr($vards, $ran_3, 1), "-"); $vards = mb_str_replace($vards, mb_substr($vards, $ran_4, 1), "-"); if($cik >= 5) $vards = mb_str_replace($vards, mb_substr($vards, $ran_5, 1), "-"); if($cik >= 6) $vards = mb_str_replace($vards, mb_substr($vards, $ran_6, 1), "-"); if($cik >= 7) $vards = mb_str_replace($vards, mb_substr($vards, $ran_7, 1), "-"); if($cik >= 8) $vards = mb_str_replace($vards, mb_substr($vards, $ran_8, 1), "-"); if($cik >= 9) $vards = mb_str_replace($vards, mb_substr($vards, $ran_9, 1), "-"); Quote Link to comment Share on other sites More sharing options...
torrentz Posted March 7, 2009 Author Report Share Posted March 7, 2009 (edited) Apnika čakarēties ar PHP un UTF-8; no sākuma pārkonvertēju uz cp1251, tad izmantoju parastās funkcijas un pēc tam konvertēju atpakaļ uz UTF-8... $ottt = iconv("UTF-8", "CP1251", $ottt); blablabla... $vards = iconv("CP1251", "UTF-8", $vards); Edited March 7, 2009 by torrentz Quote Link to comment Share on other sites More sharing options...
Paulinjsh Posted March 9, 2009 Report Share Posted March 9, 2009 mb_* nepieciešams norādīt arī kādā encodingā ir. To varam norādīt visam skriptam ar mb_internal_encoding('UTF-8'); vai arī konkrētajai funkcijai. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.