PHP 函数 substr() 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1973649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:30:54  来源:igfitidea点击:

PHP function substr() error

phpsubstr

提问by Stoikidis

When I use substr()I get a strange character at the end

当我使用substr()时,最后会出现一个奇怪的字符

$articleText = substr($articleText,0,500);

I have an output of 500 chars and ? <--

我有 500 个字符的输出和 ? <--

How can I fix this? Is it an encoding problem? My language is Greek.

我怎样才能解决这个问题?是编码问题吗?我的语言是希腊语。

回答by Pascal MARTIN

substris counting using bytes, and not characters.

substr使用字节而不是字符进行计数。

greek probably means you are using some multi-byte encoding, like UTF-8 -- and counting per bytes is not quite good for those.

greek 可能意味着您正在使用一些多字节编码,如 UTF-8 —— 并且每字节计数对那些不太好。

Maybe using mb_substrcould help, here : the mb_*functions have been created specifically for multi-byte encodings.

也许使用mb_substr会有所帮助,这里:这些mb_*函数是专门为多字节编码创建的。

回答by U?ur ?zp?nar

Use mb_substrinstead, it is able to deal with multiple encodings, not only single-byte strings as substr:

改用mb_substr它,它能够处理多种编码,而不仅仅是单字节字符串substr

$articleText = mb_substr($articleText,0,500,'UTF-8');

回答by deceze

Looks like you're slicing a unicode character in half there. Use mb_substrinstead for unicode-safe string slicing.

看起来您正在将一个 unicode 字符切成两半。使用mb_substr而不是为Unicode的安全字符串的切片。

回答by Kristoffer Bohmann

Alternative solution for UTF-8 encoded strings - this will convert UTF-8 to characters before cutting the sub-string.

UTF-8 编码字符串的替代解决方案 - 这将在切割子字符串之前将 UTF-8 转换为字符。

$articleText = substr(utf8_decode($articleText),0,500);

To get the articleText string back to UTF-8, an extra operation will be needed:

要将 articleText 字符串恢复为 UTF-8,还需要一个额外的操作:

$articleText = utf8_encode( substr(utf8_decode($articleText),0,500) );

回答by Moussawi7

use this function, It worked for me

使用这个功能,它对我有用

function substr_unicode($str, $s, $l = null) {
    return join("", array_slice(
        preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $s, $l));
}

Credits: http://php.net/manual/en/function.mb-substr.php#107698

学分:http: //php.net/manual/en/function.mb-substr.php#107698

回答by GowriShankar

You are trying to cut unicode character.So i preferred instead of substr()try mb_substr()in php.

你正在尝试削减 unicode 字符。所以我更喜欢而不是在 php中substr()尝试mb_substr()

substr()

substr()

substr ( string $string , int $start [, int $length ] )

mb_substr()

mb_substr()

mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )

For more information for substr() - Credits => Check Here

有关 substr() 的更多信息 - Credits => 在这里查看

回答by Dr Nick Engerer

ms_substr() also works excellently for removing strange trailing line breaks as well, which I was having trouble with after parsing html code. The problem was NOT handled by:

ms_substr() 也可以很好地去除奇怪的尾随换行符,我在解析 html 代码后遇到了麻烦。问题未由以下人员处理:

 trim() 

or:

或者:

 var_dump(preg_match('/^\n|\n$/', $variable));

or:

或者:

str_replace (array('\r\n', '\n', '\r'), ' ', $text)

Don't catch.

不要抓。