taith Posted November 26, 2006 Share Posted November 26, 2006 [code]<?function copycolours($string1,$string2){ $i=0; while($pos=strpos($string1,'`',$i)!==false){ $pre=substr($string2,0,$pos); $colour=substr($string1,$pos,$pos+1); $post=substr($string2,-$pos+2); $string2=$pre.$colour.$post; $i=$pos+2; } return $string2;}echo copycolours("t`fest","echo"); #i workecho copycolours("t`fe`tst","echo"); #i crash everyrthing?>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 26, 2006 Share Posted November 26, 2006 What, is this function supposed to do?Ken Quote Link to comment Share on other sites More sharing options...
taith Posted November 26, 2006 Author Share Posted November 26, 2006 basically it takes the colors(`*) (translated elsewhere) and moves them onto another string in the exact same locations...so...echo copycolours("t`fest","echo");ouptuts e`fchobut if iecho copycolours("t`fes`dt","echo");it should output e`fch`do but it loops indefinatly... Quote Link to comment Share on other sites More sharing options...
theironchef Posted November 26, 2006 Share Posted November 26, 2006 isn't it because you have 2 " ` " characters in the string that crashes it. Its finding substrings and you only want it to find one, but it returns 2 seperate one? possibly? i dunno lol Quote Link to comment Share on other sites More sharing options...
taith Posted November 26, 2006 Author Share Posted November 26, 2006 $pos=strpos($string1,'`',$i)yes.. however the $i there is an offset... it should find the first one, set the offset to the right, and search again... Quote Link to comment Share on other sites More sharing options...
theironchef Posted November 26, 2006 Share Posted November 26, 2006 throw in some echos to see what looping indefintiely. Why ru incrmeenting $i by $pos+2 and not +1? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 26, 2006 Share Posted November 26, 2006 I believe you're running into problems because of how you're changing $string2.I rewrote your function to use a third string. It also uses a simple for loop to go through $string1 looking for the backtick character.[code]<?phpfunction copycolours($string1,$string2){ $str3 = ''; $j = 0; for ($i=0;$i<strlen($string1);$i++) { if ($string1[$i] == '`') { $str3 .= $string1[$i++]; $str3 .= $string1[$i]; } else if ($j < strlen($string2)) $str3 .= $string2[$j++]; } return $str3;}echo copycolours("t`fest","echo").'<br>'; #i workecho copycolours("t`fe`tst","echo").'<br>'; #i crash everyrthingecho copycolours("t`fes`dt","echo");?>[/code]This works with the three examples you gave. No guarantee on any other strings.Ken Quote Link to comment Share on other sites More sharing options...
taith Posted November 26, 2006 Author Share Posted November 26, 2006 doesnt matter if its +1/+2and the $pos is always returning the same... offset or no... Quote Link to comment Share on other sites More sharing options...
theironchef Posted November 26, 2006 Share Posted November 26, 2006 hmm odd... i ran ur script on my server and i added the echos in myself....the $i value never chnaged form 3 the whole time... ill look into it ... lemme know if u find out Quote Link to comment Share on other sites More sharing options...
theironchef Posted November 26, 2006 Share Posted November 26, 2006 yea i added some more... the $pos ( it equals 1 btw) variable never changes so adding 2 always makes it 3.... Quote Link to comment Share on other sites More sharing options...
taith Posted November 26, 2006 Author Share Posted November 26, 2006 yours works perfectly ken... thanks! not as fast as the other one should be tho... Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 26, 2006 Share Posted November 26, 2006 For short strings, the time difference is very small. Since your original functions didn't always work, I'd say that mine in infinitely faster in some cases. :)Ken Quote Link to comment Share on other sites More sharing options...
taith Posted November 26, 2006 Author Share Posted November 26, 2006 lol true that... but in my mind... every scrap of time saved, is well worth its while... 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.