Solution of this problem
$str = trim($str, chr(0xC2).chr(0xA0))
質問
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:
\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 behaviorAccepted @Андрей-Сердюк 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);
?
$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 .']';