Consider the line segment connecting points $(x_0,y_0)$ and $(x_1,y_1)$ in the Cartesian plane, of length $\ell = \sqrt{(x_1-x_0)^2 + (y_1-y_0)^2}$.
The entire length of the line segment is interior to the square if and only if both its endpoints are interior to the square. So it makes sense to begin the determination of length with a check on which of the endpoints (if either) fall inside the square.
Let the (filled) square with axis-parallel sides be $[a,a+s] \times [b,b+s]$. A point $(x,y)$ is in the square if and only if:
$$ 0 \le x-a \le s \;\text{ and }\; 0 \le y-b \le s $$
There are three cases: both inside, one inside & one outside, both outside. Other than the first of these cases, the length you want will depend on locating intersections between line segments, a topic that has been explored here before.
In the second case (one endpoint inside, one outside), there is always a single intersection to locate (between the given line segment and one edge of the square).
In the third case (both endpoints outside) we have the most difficult checks to perform. The given line segment may have no intersection with edges, it may have distinct intersections with two edges, or it might one intersection at a corner of the square (which zero length you might consider equivalent to missing the square entirely).
There is no apparent value to trying to package all those possible outcomes into one "algebraic equation", but if you explain why you need this, it can be done with a certain amount of complexity.
lis in a general line but fixed, and the square can be anywhere in plane. In fact, as I said in question I want an algebraic equation that shows the intersection length with respect to place of squares– JalalJaberi Mar 02 '17 at 14:36