replace   characters that are hidden in text

PhpRegexUnicode

Php Problem Overview


How to remove   (that are hidden) and SPACES in below text but

  • hold UNICODE characters
  • hold <br> tag

i tested:

  • i used trim($string) => NOT WORKED

  • i used str_replace('&nbsp;', '', $string) => NOT WORKED

  • i used some regex => NOT WORKED

                  <br>تاريخ ورود: یکشنبه ۲۳ بهمن ماه ۱۳۹۰
    

UPDATE: Image of hidden   Thanks

Php Solutions


Solution 1 - Php

This solution will work, I tested it:

$string = htmlentities($content, null, 'utf-8');
$content = str_replace("&nbsp;", "", $string);
$content = html_entity_decode($content);

Solution 2 - Php

Not tested, but if you use something like:

$string = preg_replace("/\s/",'',$string);

That should remove all spaces.

UPDATE

To remove all spaces and &nbsp; references, use something like:

$string = preg_replace("/\s|&nbsp;/",'',$string);

UPDATE 2

Try this:

$string = html_entity_decode($string);

$string = preg_replace("/\s/",'',$string);

echo $string;

Forgot to say, reconvert the html entities so add this after the replacement:

htmlentities($string);

Solution 3 - Php

All solutions above kind of work, until one starts to work with German language where there are such letters:

ä &auml;

and othere simial ones. I use the following code:

$string = preg_replace ( "!\s++!u", ' ', $string );

More details here: PCRE(3) Library Functions Manual

Solution 4 - Php

This worked for me.

preg_replace("/&nbsp;/",'',$string)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBehnamView Question on Stackoverflow
Solution 1 - PhpMostafa MView Answer on Stackoverflow
Solution 2 - PhpBen CareyView Answer on Stackoverflow
Solution 3 - PhpOleksiy MuzalyevView Answer on Stackoverflow
Solution 4 - PhpFullStackEngineerView Answer on Stackoverflow