Jump to content

function issues...


taith

Recommended Posts

[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 work
echo copycolours("t`fe`tst","echo"); #i crash everyrthing
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/28520-function-issues/
Share on other sites

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`fcho

but if i
echo copycolours("t`fes`dt","echo");
it should output e`fch`do but it loops indefinatly...
Link to comment
https://forums.phpfreaks.com/topic/28520-function-issues/#findComment-130501
Share on other sites

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]<?php
function 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 work
echo copycolours("t`fe`tst","echo").'<br>'; #i crash everyrthing
echo copycolours("t`fes`dt","echo");
?>[/code]

This works with the three examples you gave.  No guarantee on any other strings.

Ken
Link to comment
https://forums.phpfreaks.com/topic/28520-function-issues/#findComment-130511
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.