When the white space became a beast
Tags: whitespace, nbsp, programming, strange
Published: 2017-05-10 15:59:58

---
title: When the white space became a beast
published: true
description: Every now and then, on the World Wide Web, someone meets the "white space beast" and it drives us crazy, we get scared, we can’t understand what our eyes see, we cannot explain what’s going on.
cover_image: http://heavydots.com/uploads/blog-post/12-featured-illustration_the_beast.jpg
tags: white space, nbsp, programming, strange
---

_This article was originally posted at [HeavyDots Blog_](http://heavydots.com/blog/when-the-white-space-became-a-beast)_

## The legend of the white space


Every now and then, on the World Wide Web, someone meets the "white space" or for us: "the beast".

When we meet the beast, it drives us crazy, we get scared, we can’t understand what our eyes see, we cannot explain what’s going on, we wonder if something affected our perception. A supernatural phenomenon is in front of our eyes and overwhelms us.

- [What is this INSANE space character??? (google chrome)](http://stackoverflow.com/questions/26962323/what-is-this-insane-space-character-google-chrome)
- [Weird space character in string, that's not a space?](http://stackoverflow.com/questions/15565479/weird-space-character-in-string-thats-not-a-space)
- [Weird white space characters - utf8 PHP](http://stackoverflow.com/questions/7262687/weird-white-space-characters-utf8-php)
- [I have a strange space character in a String](http://stackoverflow.com/questions/12733113/i-have-a-strange-space-character-in-a-string)  

We also got into it twice! Once on a website and once even inside of a Excel sheet! It’s unbelievable the places where this creature summons and the way it got there is an even bigger mystery!

## The brave who went after it

Some give up, run and hide from it. Or just accept it without questioning its existence. But others, the brave ones, start on a journey through the darkness and don’t give up until they know the truth.

This is what someone who has been there and survived confessed (with shaking hands):

> To the people in the future like myself that had to debug this from a high level all the way down to the character codes, I salute you.

> [crclayton - Sep 1 '16 at 21:22 - stackoverflow.com](http://stackoverflow.com/questions/26962323/what-is-this-insane-space-character-google-chrome/39281063#39281063)

We've also been there and we returned with the truth ready to share it with you.

## How the creature looks and where does it come from

The strange space is actually the `&nbsp;` entity ([Wikipedia](https://en.wikipedia.org/wiki/Non-breaking_space)), pretty well known by HTML coders but in this case it is not represented/encoded in HTML.

So basically most of us knew the creature, but we didn’t know it could exist in another shape and dimension.

### Wikipedia explains the different representations of the beast:

![Wikipedia explains the different representations of the beast](http://heavydots.com/uploads/blog-post/12-featured-wikipedia%20nbsp%20encodings.png)

### And also how the creature is born and mutates:

![And also how the creature is born and mutates](http://heavydots.com/uploads/blog-post/12-featured-wikipedia%20nbsp%20keyboard%20entry%20methods.png)

## The tools and techniques to find it and get rid of it

For the even braver who want to go after it and make it disappear we’ve got some tools and techniques to help locating it and still come back alive from the journey.

### Web tool that looks for it in the code of a webpage:

Here’s a tool with a web interface where you can enter a URL and it searches the contents of that page in order to find the beast:

http://tools.heavydots.com/nbsp-space-char-detect/

### Manual cleanup technique:

But if you find yourself in the dark code dimension and cannot use the web, and you have to get very close to the beast in order to kill it, here is a way to exorcise your demonized code:

Only for those who are skilled with PHP spells, run a search of `chr(194).chr(160)` and replace it with an ordinary space. This will extract the demon out of it and will restore back its clean white space soul.

Take this scroll with you, it contains the spell you will need when you face the beast: 

```php
// Define the white beast
$white_beast=chr(194).chr(160);

// Count how many of them are living in your text
$count=substr_count($string, $white_beast);
print_r($count);

// Replace it with a normal space
$string=str_replace($white_beast, ' ', $string);
```

## THE END of the story

So dear reader of this legend, if you never faced the beast, beware!

But if you have also had to deal with it, please share with us your story in the comments section! 


I'll try to post new stuff here, and also invite you to drop by at [HeavyDots Blog](http://heavydots.com/blog/)