2

I have a string in UTF-8 with non-printable space which I need to replace with non-printable linebreak

something like this str_replace('&nbsp;','<br />',$string); but with non-printable characters.

alexandresaiz
  • 2,678
  • 7
  • 29
  • 40
JTC
  • 3,344
  • 3
  • 28
  • 46
  • 2
    Non-printable space/linebreak? These things don't exist in Unicode, so I expect they would be quite difficult to search for. – r3mainer Jul 29 '15 at 09:37

2 Answers2

3

It literally works if you type in the specific character between the quotes:

str_replace(' ', '', $string)
             ^   ^^
        put characters here

Since this can arguably be a bit hard to type and/or make the source code less than obvious, you can write those string literals in their byte notation. Just figure out what specific character you're talking about and what bytes it's encoded in:

str_replace("\xE2\x80\xAF", "\x0A", $string)

This is replacing a ZERO-WIDTH SPACE (UTF-8 encoding E2 80 AF) with a regular line feed (0A). Look it up in your Unicode table of choice. Possibly inspect your existing string using echo bin2hex($string) to figure out what bytes it contains.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • The first snippet works for me like charm :), I just added in `$replace` parameter a line-break with enter keyobard and it worked. Thanks. – JTC Jul 30 '15 at 10:18
  • for anyone having a troublesome time when preg_replace doesn't pick it up when it should, this works like a charm! I just added in the hex editor values of the invisible space e2 80 8b in my case and it actually worked. after a lot of things had failed, this is the first solution that straight up works for me – Tschallacka Mar 10 '16 at 13:02
0

How about: str_replace('&nbsp;' , PHP_EOL , $string);

Or even better: str_replace( array( '&nbsp;', ' ' ) , PHP_EOL , $string);

Sorin C
  • 984
  • 10
  • 8