質問

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.

役に立ちましたか?

解決

Solution of this problem

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

他のヒント

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à"

How about:

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

?

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

$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 .']';
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top