Unneded number combination after replacing a number in a string
I get an unneeded number combination.
($_COOKIE):
2, 3, 4, 5, 6, 7, 8, 901234567890123456789, 30
Should be ($_COOKIE):
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12... (till) 30
$_Get['id']="1"; (sorry, forgot to post it.)
Why that happens?
The code:
<?
ob_start();
$id=$_GET['id'];
if (!empty($id)){
$id=str_replace('a9_','', $id);
$value=$_COOKIE['NaudingasURL'];
$exp = explode(", ", $value);
if(in_array($id, $exp)){
$value2=str_replace(', '.$id,"", ', '.$value);
$value2=substr($value2, 2, strlen($value2));
echo'r';
}
else{
$value2=$value.', '开发者_如何学编程.$id; echo'a';
}
setcookie("NaudingasURL", $value2);
}
ob_end_flush();
?>
I'm calling it with Jquery ajax, but I don't thinks that's the problem.
You are replacing every ",1" with and empty string. So 10 will be 0 and so on...
But i dont understand what exactly you want to achieve?
Ok, if it was what Max said, that you could do it like this:
$exp = explode(", ", $value);
if(in_array($id, $exp)){
for ($i=0; $i<count($exp); $i++) {
if ($exp[$i] == $id) {
unset($exp[$i]);
}
}
$value2 = implode(", ", $exp);
}
else{
$value2 = implode(", ", $exp).', '.$id;
}
精彩评论