1

I need delete all tags from string and make it without spaces.

I have string

"<span class="left_corner"> </span><span class="text">Adv</span><span class="right_corner"> </span>"

After using strip_tags I get string

" Adv "

Using trim function I can`t delete spaces.

JSON string looks like "\u00a0...\u00a0".

Help me please delete this spaces.

2
  • 1
    Are you using trim before or after stripping the tags?
    – StephenTG
    Commented Aug 12, 2013 at 18:28
  • Those problematic characters are called "non breaking spaces". The solution Oskar wrote should work (using preg_replace).
    – Itay
    Commented Aug 12, 2013 at 18:36

5 Answers 5

7

Solution of this problem

$str = trim($str, chr(0xC2).chr(0xA0))

2
  • This one is the only one that truly worked for me. My string had some odd spacing characters!
    – Patareco
    Commented Feb 5, 2014 at 15:21
  • This solution is not multibyte-safe, it will break encoding of your UTF8 strings (see my answer to the question).
    – e1v
    Commented Oct 2, 2016 at 9:50
4

You should use preg_replace(), to make it in multibyte-safe way.

$str = preg_replace('/^[\s\x00]+|[\s\x00]+$/u', '', $str);

Notes:

  • this will fix initial @Андрей-Сердюк's problem: it will trim \u00a0, because \s matches Unicode non-breaking spaces too
  • /u modifier (PCRE_UTF8) tells PCRE to handle subject as UTF8-string
  • \x00 matches null-byte characters to mimic default trim() function behavior

Accepted @Андрей-Сердюк trim() answer will mess with multibyte strings.

Example:

// This works:
echo trim(' Hello ', ' '.chr(0xC2).chr(0xA0)); 
// > "Hello" 

// And this doesn't work:
echo trim('  Solidarietà  ', ' '.chr(0xC2).chr(0xA0)); 
// > "Solidariet?" -- invalid UTF8 character sequense

// This works for both single-byte and multi-byte sequenses:
echo preg_replace('/^[\s\x00]+|[\s\x00]+$/u', '', ' Hello ');
// > "Hello"
echo preg_replace('/^[\s\x00]+|[\s\x00]+$/u', '', ' Solidarietà '); 
// > "Solidarietà"
1
  • 1
    After 4h of searching around, this seems to be the solution to go with!
    – Bram
    Commented Jan 19, 2018 at 12:37
0

How about:

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

?

http://php.net/manual/en/function.preg-replace.php

1
  • It makes my string null and delete all spaces between other words if that exists. Commented Aug 12, 2013 at 19:06
0

I was using the accepted solution for years and I've been wrong all this time. If I can find this solution in 2022, others too, so please change the accepted solution to the one from @e1v who was right all this time.

You SHOULD NOT DO THIS!

echo trim('Au delà', ' '.chr(0xC2).chr(0xA0));

As it corrupts the UTF-8 encoding:

Au del�

Note that a "modern" (PHP 7) way to write this could be:

echo trim('Au delà', " \u{a0}");//This is WRONG, don't do it!

Personally, when I have to deal with non breakable spaces (Unicode 00A0, UTF8 C2A0) in strings, I replace the trailing/ending ones by regular spaces (Unicode 0020, UTF8 20), and then trim the string. Like this:

echo trim(preg_replace('/^\s+|\s+$/u', ' ', "Au delà\u{a0}"));

(I would have post a comment or just vote the answer up, but I can't).

-1
$str = '<span class="left_corner"> </span><span class="text">Adv</span><span class="right_corner"> </span>';
$rgx = '#(<[^>]+>)|(\s+)#';
$cleaned_str = preg_replace( $rgx, '' , $str );
echo '['. $cleaned_str .']';
1
  • It makes my string as null. And with for other strings it delete all spaces between words. Commented Aug 13, 2013 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.