Solomon Closson
Solomon Closson

Reputation: 6217

PHP fix zero-width-space inside string variable

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?

Upvotes: 2

Views: 3872

Answers (2)

Philipp
Philipp

Reputation: 15629

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);
}

Upvotes: 4

Solomon Closson
Solomon Closson

Reputation: 6217

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!

Upvotes: 3

Related Questions