anonīms Posted August 5, 2011 Report Share Posted August 5, 2011 Sveiki. Kā varētu aizvietot stringu ar ****, atstājot tikai pirmo un pēdējo burtu? ir jau gatava fja vai likt lietā substr? atlasot, pirmo un pēdējo, tad visus burtus samainot pret * un pielikt šos burtus? Quote Link to comment Share on other sites More sharing options...
daGrevis Posted August 5, 2011 Report Share Posted August 5, 2011 Es mēģinātu kā ar 'substr()'. Quote Link to comment Share on other sites More sharing options...
codez Posted August 5, 2011 Report Share Posted August 5, 2011 (edited) Atkal uzdevums, kurš uztaisīs īsāko funkciju? function f($s){return str_pad($s[0],$l=strlen($s)-1,'*').$s[$l];} Edited August 5, 2011 by codez Quote Link to comment Share on other sites More sharing options...
anonīms Posted August 5, 2011 Author Report Share Posted August 5, 2011 who is the man? codez ;D Paldies un Tev cepums Quote Link to comment Share on other sites More sharing options...
Maris-S Posted August 5, 2011 Report Share Posted August 5, 2011 Tā tīri ienāca prātā pārbaudīt kā strādās rakstzīmju virkne, ja tās elementus paņem kā no masīva. Man izskatās ka ar utf-8 simboliem nepareizi strādā. Vai kaut ko darīju nepareizi? $str = "Ābols"; echo($str[0]); Quote Link to comment Share on other sites More sharing options...
daGrevis Posted August 5, 2011 Report Share Posted August 5, 2011 Man iešāvās prātā, ka viens šejienietis teica: 'Ja Mēs skatāmies kā C++ to implementē - jebkurš strings īstenībā ir masīvs'. Ir funkcija 'str_split()', kas neatbalsta UTF-8 un ir, tur pat - komentāros, uzrakstīta funkcija, kas to atbalsta: function str_split_php4_utf8($str) { // place each character of the string into and array $split=1; $array = array(); for ( $i=0; $i < strlen( $str ); ){ $value = ord($str[$i]); if($value > 127){ if($value >= 192 && $value <= 223) $split=2; elseif($value >= 224 && $value <= 239) $split=3; elseif($value >= 240 && $value <= 247) $split=4; }else{ $split=1; } $key = NULL; for ( $j = 0; $j < $split; $j++, $i++ ) { $key .= $str[$i]; } array_push( $array, $key ); } return $array; } Quote Link to comment Share on other sites More sharing options...
briedis Posted August 5, 2011 Report Share Posted August 5, 2011 (edited) mhm, codez variants nav utf-8 proof :p function getStarred($s){ mb_internal_encoding("UTF-8"); if($l = mb_strlen($s) < 3){ return $s; } return mb_substr($s, 1, 1) . str_repeat('*', $l - 2) . mb_substr($s, -1); } ..mans gan ir.. EDIT: daGrevis: mb_str_split($s){ $a = array(); for($i=0;$i<=mb_strlen($s)-1;$i++){ $a[] = mb_substr($s, $i, 1); } return $a; } Edited August 5, 2011 by briedis Quote Link to comment Share on other sites More sharing options...
daGrevis Posted August 5, 2011 Report Share Posted August 5, 2011 Kūl, briedi. :) Quote Link to comment Share on other sites More sharing options...
briedis Posted August 5, 2011 Report Share Posted August 5, 2011 Vienīgi tur jāskatās, vai mb_* bibliotēka ir pieslēgta. Moš tajā php4 vēl nebija labs atbalsts... Quote Link to comment Share on other sites More sharing options...
daGrevis Posted August 5, 2011 Report Share Posted August 5, 2011 Piekāst PHP 4. Quote Link to comment Share on other sites More sharing options...
spainis Posted August 5, 2011 Report Share Posted August 5, 2011 Atkal uzdevums, kurš uztaisīs īsāko funkciju? function f($s){return str_pad($s[0],$l=strlen($s)-1,'*').$s[$l];} PHP_FUNCTION(magic_replace) { char* text; int text_length; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &text, &text_length) == FAILURE) { return; } memset(text + 1, '*', text_length -2); RETURN_STRING(text, 1); } function f($s){return str_pad($s[0],$l=strlen($s)-1,'*').$s[$l];} $loops = 100000; $string = str_pad('', 10000, '0'); $timer = new Timer(); $timer->start(); for($i = 0; $i < $loops; $i++){ f($string); } $timer->stop(); echo 'f()' . $timer . "\n"; $timer->start(); for($i = 0; $i < $loops; $i++){ magic_replace($string); } $timer->stop(); echo 'magic_replace() ' . $timer . "\n"; f()15.173057079315 magic_replace() 0.45169615745544 :P Quote Link to comment Share on other sites More sharing options...
daGrevis Posted August 6, 2011 Report Share Posted August 6, 2011 Kas tie par baltās ķēves melnajiem murgiem? Quote Link to comment Share on other sites More sharing options...
briedis Posted August 6, 2011 Report Share Posted August 6, 2011 Kas tie par baltās ķēves melnajiem murgiem? Es teiktu, ka C vai C++ paškodēta php funkcija :) Laikam vairāk šeit: http://devzone.zend.com/article/1021 Quote Link to comment Share on other sites More sharing options...
codez Posted August 6, 2011 Report Share Posted August 6, 2011 spainis showed some mad coding skilzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz. Quote Link to comment Share on other sites More sharing options...
briedis Posted August 7, 2011 Report Share Posted August 7, 2011 (edited) spainis showed some mad coding skilzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz. Bet vai viņa variants ir utf8 proof? :) char tak ir tikai baits... Edited August 7, 2011 by briedis 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.