[英]PHP trim unexpected behaviour
I am using the following function in PHP to trim some unwanted characters. 我在PHP中使用以下函数来修剪一些不需要的字符。
$inputString = "आनन्द मठ";
trim(html_entity_decode($inputString), " \t\n\r\0\x0B\xC2\xA0");
The above code is working fine for all cases but in one input string ( आनन्द मठ
) it is converting it to आनन्द म
. 上面的代码在所有情况下都可以正常工作,但在一个输入字符串(
आनन्द मठ
)中,它将其转换为आनन्द म
。 It has a unwanted . 它有不需要的。 Also happening for
परेटो- श्रेष्ठ
converted to परेटो- श्रेष्
. परेटो- श्रेष्ठ
转换为परेटो- श्रेष्
。
trim()
This function use iso-8859 encoding. 此功能使用iso-8859编码。
you must use UTF8 (Unicode) function. 您必须使用UTF8(Unicode)函数。 Try this function
试试这个功能
function mb_trim($string, $charlist='\\\\s', $ltrim=true, $rtrim=true)
{
$both_ends = $ltrim && $rtrim;
$char_class_inner = preg_replace(
array( '/[\^\-\]\\\]/S', '/\\\{4}/S' ),
array( '\\\\\\0', '\\' ),
$charlist
);
$work_horse = '[' . $char_class_inner . ']+';
$ltrim && $left_pattern = '^' . $work_horse;
$rtrim && $right_pattern = $work_horse . '$';
if($both_ends)
{
$pattern_middle = $left_pattern . '|' . $right_pattern;
}
elseif($ltrim)
{
$pattern_middle = $left_pattern;
}
else
{
$pattern_middle = $right_pattern;
}
return preg_replace("/$pattern_middle/usSD", '', $string) );
}
Add http header in your php like 在您的php中添加http标头
header("Content-Type: text/html; charset=ISO-8859-1");
or put the encoding in a meta tag: 或将编码放入meta标签中:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.