The usual way of doing this is to separate the collision checks depending on the acceleration sign :
// player moving to the right
if(xacc > 0 && collision) xplayer = xwall - widthplayer;
// player moving to the left
if(xacc < 0 && collision) xplayer = xwall;
It'll works great for any speed lower than your object/tile width
So you mean it gets stuck in the wall because your collision detection doesn't allow it to move out of the wall again?
I tend to check whether the next move would cause a collision and if it does, I'd resolve it by moving it as close to the wall as allowed. That way you never move the character beyond the allowed boundary and thus it can't get stuck.
This is how I do it too. Pseudocode:
// this moves the player to where he would be next frame
player.move(player.velocity);
// check for collision on next frame's position
if (player.collidesWithWall(wall)) {
// move player back to position he was in (that is; not colliding with the wall)
player.move(-player.velocity); // note the negative sign
// slowly move player towards wall until they hit it
while (!player.collidesWithWall(wall)) {
player.move({tiny distance towards wall, maybe one pixel});
}
// player is now inside the wall, move them out
player.move(-{same tiny distance as above}); // note the negative sign
// player is now right up next to the wall. stop movement.
player.velocity = Vector(0, 0);
}