2

I have zipcodes being outputted, coming from user inputted values. Looks like it is outputting zero-width-space \u200b sometimes at the beginning of the strings.

What is the best way to replace these from within php before echoing the variable?

Solomon Closson
  • 6,111
  • 14
  • 73
  • 115
  • holy jsfiddle code snippet – Abdul Ahmad Oct 22 '15 at 04:09
  • Possible duplicate of [Why is "​" being injected into my HTML?](http://stackoverflow.com/questions/18478847/why-is-8203-being-injected-into-my-html) – Jack Oct 22 '15 at 04:10
  • Don't have time to investigate this further, but it looks like this post is closely related. http://stackoverflow.com/questions/18478847/why-is-8203-being-injected-into-my-html – Sean Montana Oct 22 '15 at 04:10
  • @AbdulAhmad - lol, yes, it is quite a doozy... sorry about that, but it is difficult to break apart also. – Solomon Closson Oct 22 '15 at 04:10
  • @JackPattishall - I have seen that question all ready, the answer is not entirely clear. Basically just says the browser interprets it as a zero-width-space and that does not really provide a clear answer as to what the root cause of the problem is. – Solomon Closson Oct 22 '15 at 04:14
  • @SolomonClosson see my answer,it sorts correctly now. – zer00ne Oct 22 '15 at 04:21
  • Looks like the html is being outputted with `\u200b`, but how to get this with php or js? – Solomon Closson Oct 22 '15 at 04:24
  • Interestingly enough, if I take the HTML from JSFiddle, and then copy a row with the invalid character (zip code with 07439, for instance), I get the following: http://jsfiddle.net/rjkkv3wv/5/ - Note, NOTHING has been changed on my end. What IDE are you using? – Jack Oct 22 '15 at 04:27
  • Using the hex viewer in the original link, I can view the characters. Looks like you solved the problem though in your answer :) – Jack Oct 22 '15 at 04:38
  • I'm hoping that my answer should fix this issue. Seems like it fixes it, not sure if there is a better way to do this, but this is working for now. – Solomon Closson Oct 22 '15 at 04:44
  • Edited question, sorry for all the trouble here guys. Please let me know if there is a better way to replace these zero-width-space chars within php, other than my answer provided. If there is, I will be glad to accept the other answer. I'm not that skilled in doing this in php. – Solomon Closson Oct 22 '15 at 04:50

2 Answers2

4

I use this function to trim unicode spaces - this should work in your case too.

function trimUnicode($str) {
    return preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u','',$str);
}
Philipp
  • 15,377
  • 4
  • 35
  • 52
3

Ok, seems as though this was coming from the actual string being echo'd by PHP, so I did the following to the string:

$zipcode = trim(utf8_decode($zipcode), '?');

All seems fine now!

Solomon Closson
  • 6,111
  • 14
  • 73
  • 115