PHPutf8编码解码
PHP utf8 encoding and decoding
我在php
中有以下代码
$test = "1326";
echo utf8_decode($test);
var_dump($test);
我得到以下结果:
isbn
string(4) "isbn"
我从包含 1326 文本
的 txt 文件中获取了一些文本
$all_text = file_get_contents('test.txt');
var_dump($all_text);
结果:
string(16) "1326"
我有以下问题:
如何使用 utf8 解码第二个文本以便获得 isbn 结果?
如何编码 isbn 以获得 1326?
编辑
(来自评论)
我用 iconv 和编码尝试了所有方法,但没有任何效果。 .txt 文件中的文本是字符串 (16) 而不是字符串 (4),因此我可以对其进行编码。 txt文件是用西方(ISO 8859-1)编码从sublime保存的
尝试使用 stripcslashes
:
<?php
$test = "1326";
echo utf8_decode( $test ); // "isbn"
var_dump( $test );
echo "<br/><br/><br/>";
$all_text = file_get_contents( "test.txt" );
echo utf8_decode( $all_text ) . // "1326"
"<br/>" .
utf8_decode( stripcslashes( $all_text ) ); // "isbn"
var_dump( stripcslashes( $all_text ) );
?>
使用此文件测试:
This is some text :
1326
And this is more text!!!
接下来是如何将字符转换为代码:
<?php
$test = "isbn";
$coded = "";
for ( $i = 0; $i < strlen( $test ); $i++ ) // PROCESS EACH CHAR IN STRING.
$coded .= "\" . decoct( ord( $test[ $i ] ) ); // CHAR CODE TO OCTAL.
echo $coded . // "1326"
"<br/>" .
stripcslashes( $coded ); // "isbn".
?>
让我们用一个我们可以在任何地方调用的函数让它更通用:
<?php
function code_string ( $s )
{ $coded = "";
for ( $i = 0; $i < strlen( $s ); $i++ )
$coded .= "\" . decoct( ord( $s[ $i ] ) );
return $coded;
}
$x = code_string( "isbn" );
echo $x . // "1326"
"<br/>" .
stripcslashes( $x ); // "isbn".
?>
这与 UTF-8 编码完全无关。 完全忘记那部分。 utf8_decode
不会在您的代码中执行任何操作。 iconv
完全无关。
它与PHP 字符串文字解释 有关。 "1326"
中的\...
是一个特殊的PHP字符串文字转义序列:
\[0-7]{1,3}
the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. "0" === "[=21=]0")
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
这很容易解释为什么它在用 PHP 字符串文字编写时有效,而在从外部源读取时不起作用(因为通过 file_get_contents
读取的外部文本未被解释为 PHP 代码)。只需执行 echo "1326"
,您将看到“isbn”,无需任何其他转换。
手动将字符串 1326
中的各个转义序列转换为它们的字符等价物(实际上:它们的字节等价物):
$string = '1326'; // note: single quotes cause no iterpretation
echo preg_replace_callback('/\\([0-7]{1,3})/', function ($m) {
return chr(octdec($m[1]));
}, $string)
// isbn
stripcslashes
恰好包含此功能,但它还执行许多其他可能不需要的事情。
反之:
$string = 'isbn';
preg_replace_callback('/./', function ($m) {
return '\' . decoct(ord($m[0]));
}, $string)
// 1326
我在php
中有以下代码$test = "1326";
echo utf8_decode($test);
var_dump($test);
我得到以下结果:
isbn
string(4) "isbn"
我从包含 1326 文本
的 txt 文件中获取了一些文本$all_text = file_get_contents('test.txt');
var_dump($all_text);
结果:
string(16) "1326"
我有以下问题:
如何使用 utf8 解码第二个文本以便获得 isbn 结果?
如何编码 isbn 以获得 1326?
编辑
(来自评论)
我用 iconv 和编码尝试了所有方法,但没有任何效果。 .txt 文件中的文本是字符串 (16) 而不是字符串 (4),因此我可以对其进行编码。 txt文件是用西方(ISO 8859-1)编码从sublime保存的
尝试使用 stripcslashes
:
<?php
$test = "1326";
echo utf8_decode( $test ); // "isbn"
var_dump( $test );
echo "<br/><br/><br/>";
$all_text = file_get_contents( "test.txt" );
echo utf8_decode( $all_text ) . // "1326"
"<br/>" .
utf8_decode( stripcslashes( $all_text ) ); // "isbn"
var_dump( stripcslashes( $all_text ) );
?>
使用此文件测试:
This is some text :
1326
And this is more text!!!
接下来是如何将字符转换为代码:
<?php
$test = "isbn";
$coded = "";
for ( $i = 0; $i < strlen( $test ); $i++ ) // PROCESS EACH CHAR IN STRING.
$coded .= "\" . decoct( ord( $test[ $i ] ) ); // CHAR CODE TO OCTAL.
echo $coded . // "1326"
"<br/>" .
stripcslashes( $coded ); // "isbn".
?>
让我们用一个我们可以在任何地方调用的函数让它更通用:
<?php
function code_string ( $s )
{ $coded = "";
for ( $i = 0; $i < strlen( $s ); $i++ )
$coded .= "\" . decoct( ord( $s[ $i ] ) );
return $coded;
}
$x = code_string( "isbn" );
echo $x . // "1326"
"<br/>" .
stripcslashes( $x ); // "isbn".
?>
这与 UTF-8 编码完全无关。 完全忘记那部分。 utf8_decode
不会在您的代码中执行任何操作。 iconv
完全无关。
它与PHP 字符串文字解释 有关。 "1326"
中的\...
是一个特殊的PHP字符串文字转义序列:
\[0-7]{1,3}
the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. "0" === "[=21=]0")http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
这很容易解释为什么它在用 PHP 字符串文字编写时有效,而在从外部源读取时不起作用(因为通过 file_get_contents
读取的外部文本未被解释为 PHP 代码)。只需执行 echo "1326"
,您将看到“isbn”,无需任何其他转换。
手动将字符串 1326
中的各个转义序列转换为它们的字符等价物(实际上:它们的字节等价物):
$string = '1326'; // note: single quotes cause no iterpretation
echo preg_replace_callback('/\\([0-7]{1,3})/', function ($m) {
return chr(octdec($m[1]));
}, $string)
// isbn
stripcslashes
恰好包含此功能,但它还执行许多其他可能不需要的事情。
反之:
$string = 'isbn';
preg_replace_callback('/./', function ($m) {
return '\' . decoct(ord($m[0]));
}, $string)
// 1326