Let $X$ be a topological space, $A,B\subseteq X$, and $f\in C(X,\mathbb R)$. Suppose further that $f(A)$ and $f(B)$ are contained in disjoint open neighborhoods of $\mathbb R$. Is this enough to enough to ensure that $A$ and $B$ are separated by a function, i.e., that there exists some $g\in C(X,\mathbb R)$ such that $g(A)=\{0\}$ and $g(B)=\{1\}$? Please give a proof or counterexample.
Also, does the answer change if we assume $A$ and $B$ are closed? What if $A$ is closed and $B$ is a singleton? I'm asking because I recently looked up the definition of Tychonoff spaces and wondered if we could give an equivalent definition along the above lines.
It seems intuitively plausible to me that this is the case, but I'm guessing it's false because none of the definitions of "separation by a function" that I can find mention this.