我想知道下面的句子对我们的假人来说简单来说意味着什么?
什么是字节序列?一个字节中有多少个字符?
iconv_strlen() counts the occurrences of characters in the given byte sequence str on the basis of the specified character set, the result of which is not necessarily identical to the length of the string in byte.
我使用iconvPHP函数,但有些字符无法正确转换:
...
$s = iconv('UTF-16', 'UTF-8', $s);
...
$s = iconv('UTF-16//IGNORE', 'UTF-8', $s);
...
$s = iconv('UTF-16LE', 'UTF-8', $s);
...
$s = iconv('UTF-16LE//IGNORE', 'UTF-8', $s);
...
Run Code Online (Sandbox Code Playgroud)
我也尝试mb_convert_encoding功能,但无法解决我的问题.
示例文本文件:9px.ir/utf8-16LE.rar
我在用户输入上使用以下代码时遇到一些问题:
htmlentities($string, ENT_COMPAT, 'UTF-8');
Run Code Online (Sandbox Code Playgroud)
当检测到无效的多字节字符时,PHP会发出通知:
PHP警告:htmlentities():第123行的/path/to/file.php中的参数中的无效多字节序列
我的第一个想法是压制错误,但这是缓慢而糟糕的做法:http: //derickrethans.nl/five-reasons-why-the-shutop-operator-should-be-avoided.html
我的第二个想法是使用ENT_IGNORE标志,但即使是PHP手册也建议不要使用它:
无声地丢弃无效的代码单元序列,而不是返回空字符串.不鼓励使用此标志,因为它可能具有安全隐患.
还有一点原因让我得到了以下代码:
// detect encoding
$encoding = mb_detect_encoding($query);
if($encoding != 'UTF-8') {
$query = mb_convert_encoding($query, 'UTF-8', $encoding);
} else {
// strip out invalid utf8 sequences
$query = iconv('UTF-8', 'UTF-8//IGNORE', $query);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,iconv 在删除/忽略无效字符时也会抛出E_NOTICE:
如果将字符串// TRANSLIT附加到out_charset,则会激活音译.这意味着当一个角色无法在目标字符集中表示时,它可以通过一个或几个相似的字符来近似.如果附加字符串// IGNORE,则会无提示地丢弃无法在目标字符集中表示的字符.否则,从第一个非法字符中删除str,并生成E_NOTICE.
所以我基本上没有选择.我宁愿使用一个久经考验的库来处理这种东西,而不是尝试使用我见过的一些基于正则表达式的解决方案.
因此,这引出了我的最后一个问题: 如何在没有通知/警告/错误的情况下有效,安全地删除无效的多字节字符?
我有一堆正在处理的文本/ html文档
其中一些包含编码的html实体,我试图将其转换为原始解码的utf字符.
这很容易使用html_entity_decode,但是,某些实体是无效的,例如
򙦙
Run Code Online (Sandbox Code Playgroud)
出于这个原因,我正在使用正则表达式来提取每个单独的实体,然后尝试以某种方式验证它们.
如果一个实体无效,我想把它保留򙦙在文档中,但像编码的东西&仍然会变成&.
只是一些示例测试代码我敲了..
<?php
function dump_chars($s)
{
if (preg_match_all('/&[#A-Za-z0-9]+;/', $s, $matches))
{
foreach ($matches[0] as $m)
{
$decoded = html_entity_decode($m, ENT_QUOTES, "UTF-8");
echo "[" . htmlentities($m, ENT_QUOTES, "UTF-8") . "] ";
echo "Decoded: [" . $decoded . "] ";
echo "Hex: [" . bin2hex($decoded) . "] ";
echo "detect: [" . mb_detect_encoding($decoded) . "]";
echo "<br>";
}
}
}
$payload = "" & ͉ ’ 򙦙";
echo …Run Code Online (Sandbox Code Playgroud) 我正在尝试将字符串从ISO-8859-1编码转换为UTF-8,但我似乎无法使其工作.这是我在irb中所做的一个例子.
irb(main):050:0> string = 'Norrlandsvägen'
=> "Norrlandsvägen"
irb(main):051:0> string.force_encoding('iso-8859-1')
=> "Norrlandsv\xC3\xA4gen"
irb(main):052:0> string = string.encode('utf-8')
=> "Norrlandsvägen"
Run Code Online (Sandbox Code Playgroud)
我不知道为什么 Norrlandsvägen在ISO-8859-1将被转换成Norrlandsvägen在UTF-8.
我尝试过编码,编码!,编码(destinationEncoding,originalEncoding),iconv,force_encoding,以及我能想到的各种奇怪的解决方法,但似乎没什么用.有人可以帮助我/指出我正确的方向吗?
Ruby新手仍然像疯了一样拉头发,但感谢所有回复... :)
这个问题的背景:我正在编写一个宝石,它将从一些网站下载一个xml文件(将具有iso-8859-1编码)并将其保存在存储中,我想先将其转换为utf-8.但像Norrlandsvägen这样的词语让我感到困惑.真的任何帮助将不胜感激!
[更新]:我意识到在irb控制台中运行这样的测试可能会给我不同的行为,所以这里是我在实际代码中的内容:
def convert_encoding(string, originalEncoding)
puts "#{string.encoding}" # ASCII-8BIT
string.encode(originalEncoding)
puts "#{string.encoding}" # still ASCII-8BIT
string.encode!('utf-8')
end
Run Code Online (Sandbox Code Playgroud)
但最后一行给出了以下错误:
Encoding::UndefinedConversionError - "\xC3" from ASCII-8BIT to UTF-8
Run Code Online (Sandbox Code Playgroud)
感谢@Amadan在下面的回答,我注意到\xC3如果你运行,实际上会显示在irb中:
irb(main):001:0> string = 'ä'
=> "ä"
irb(main):002:0> string.force_encoding('iso-8859-1')
=> "\xC3\xA4"
Run Code Online (Sandbox Code Playgroud)
我还尝试为结果分配一个新变量,string.encode(originalEncoding)但得到了一个更奇怪的错误:
newString = string.encode(originalEncoding)
puts "#{newString.encoding}" # can't even get to …Run Code Online (Sandbox Code Playgroud) 我已将Mac升级到macOS 10.12.0 Sierra,并且我正在尝试将PHP升级到7.0.9版,但"make test"失败了:
Undefined symbols for architecture x86_64:
"_libiconv", referenced from:
_zif_iconv_substr in iconv.o
_zif_iconv_mime_encode in iconv.o
_php_iconv_string in iconv.o
__php_iconv_strlen in iconv.o
__php_iconv_strpos in iconv.o
__php_iconv_appendl in iconv.o
_php_iconv_stream_filter_append_bucket in iconv.o
...
"_libiconv_close", referenced from:
_zif_iconv_substr in iconv.o
_zif_iconv_mime_encode in iconv.o
_php_iconv_string in iconv.o
__php_iconv_strlen in iconv.o
__php_iconv_strpos in iconv.o
__php_iconv_mime_decode in iconv.o
_php_iconv_stream_filter_factory_create in iconv.o
...
"_libiconv_open", referenced from:
_zif_iconv_substr in iconv.o
_zif_iconv_mime_encode in iconv.o
_php_iconv_string in iconv.o
__php_iconv_strlen in iconv.o
__php_iconv_strpos in iconv.o
__php_iconv_mime_decode in iconv.o …Run Code Online (Sandbox Code Playgroud) 我有一个MySQL表,其中120,000行以UTF-8格式存储.有一个字段,产品名称,包含带有许多重音的文本.在将其转换为友好的URL形式(ASCII)后,我需要使用相同的名称填充第二个字段.
由于PHP不直接处理UTF-8,我使用:
$value = iconv ('UTF-8', 'ISO-8859-1', $value);
将名称转换为ISO-8859-1,然后是一个巨大的strstr语句,用其非重音等效项替换任何重音字符(例如,à变为a).
但是,原始文本名称是使用智能引号输入的,而且每当遇到一个时,iconv会发出窒息 - 我得到:
Unknown error type: [8] iconv() [function.iconv]: Detected an illegal character in input string
为了在使用iconv之前删除智能引号,我尝试使用三个语句,如:
$value = str_replace('’', "'", $value);
(是UTF-8智能单引号的原始值)
因为文本文件太长,所以这些str_replace会导致脚本每次都超时.
在运行iconv之前,从UTF-8字符串中删除智能引号(或任何无效字符)的最快方法是什么?
或者,这个问题是否有更简单的解决方案?将具有多个重音符号(UTF-8)的名称转换为没有重音符号,拼写正确的ASCII格式的最快方法是什么?
我正在努力让MinGW和MSYS工作,所以我可以在Windows中构建iconv和libxml2,但是我发现./configure和make会给出很多与BSD/Unix相关的错误,这些错误不够具体到google,而不是描述性足以让我弄清楚.任何人都可以通过一些步骤来获得在Win32机器上构建的iconv和libxml2 .dll/.lib吗?
我正在为一些软件更新库,我今天整天都在忙着解决这个问题并且没有想到它.我得到了最新的libpng,zlib和curl,没有任何问题,但我发现这些库没有太多支持或操作方法,就像其他库一样.任何帮助将非常感谢,提前感谢您的时间.
詹姆士
我有一些UTF-8内容,包括多字节智能引号字符.我发现这段代码很容易将这些字符转换成ASCII直引号(ASCII码34):
$content = iconv("UTF-8", "ASCII//TRANSLIT", $content);
Run Code Online (Sandbox Code Playgroud)
要么
$content = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $content);
Run Code Online (Sandbox Code Playgroud)
但是,我宁愿将它们转换为扩展的ASCII智能引号(拉丁语1编码中的ASCII代码147和148).有谁知道如何做到这一点?
通过以下命令安装 Rust 和 Cargo 后...
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Run Code Online (Sandbox Code Playgroud)
我运行cargo build了一个小型的“Hello World”Rust 项目并收到以下错误:
= note: ld: library not found for -liconv
collect2: error: ld returned 1 exit status
error: could not compile `hello_world` due to previous error
Run Code Online (Sandbox Code Playgroud)
我尝试rustup self uninstall通过安装 Rust 和 Cargo brew,但在尝试构建时遇到相同的错误。
我运行的是 macOS Big Sur 11.6.4。
iconv ×10
php ×6
utf-8 ×3
encoding ×2
c++ ×1
iso-8859-1 ×1
libiconv ×1
libxml2 ×1
macos ×1
macos-sierra ×1
mingw ×1
msys ×1
mysql ×1
ruby ×1
rust ×1
rust-cargo ×1
smart-quotes ×1