【发布时间】:2013-02-10 14:35:34
【问题描述】:
这是我的代码:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$form_num = $_GET['num'];
echo '<form name="creative" action = "'.htmlentities($_SERVER['PHP_SELF']).'?num='.$form_num.'" method="POST" >';
echo '<table>';
for ( $i=0; $i < $form_num ; $i++ ) {
echo "<tr><td>Title: </td><td><input type='text' name='title$i' /></td></tr>";
echo "<tr><td>Description: </td><td><input type='text' name='desc$i' /></td></tr>";
echo "<tr><td> </td></tr>";
}
echo "<tr><td><input type='submit' name='submit' value='Update' /></td>
<td><input type='reset' value='Reset' /></td></tr>";
echo '</table>';
echo '</form>';
if (isset($_REQUEST['submit'])) {
require 'ad.php';
$ad = new adnetwork( $hostname, $user, $password, $database );
echo 'Status is active and set notifications are : <br /><br />';
for ($i=0; $i< $form_num; $i++) {
$params[] = array(
'title' => $ad->utf8_to_unicode(html_entity_decode($_REQUEST["title$i"], ENT_COMPAT, "UTF-8")),
'description' => $ad->utf8_to_unicode(html_entity_decode($_REQUEST["desc$i"], ENT_COMPAT, "UTF-8")),
}
file_put_contents( dirname(__FILE__) . '/' . 'get_text_in_test.json', json_encode( $params ) );
}
?>
在这里,当我使用将输入数字代码解析为字符代码的函数提交表单时:
public function utf8_to_unicode( $str ) {
$unicode = array();
$values = array();
$lookingFor = 1;
for ($i = 0; $i < strlen( $str ); $i++ ) {
$thisValue = ord( $str[ $i ] );
if ( $thisValue < ord('A') ) {
// exclude 0-9
if ($thisValue >= ord('0') && $thisValue <= ord('9')) {
// number
$unicode[] = chr($thisValue);
}
else {
$unicode[] = ''.chr($thisValue);
}
} else {
if ( $thisValue < 128)
$unicode[] = $str[ $i ];
else {
if ( count( $values ) == 0 ) $lookingFor = ( $thisValue < 224 ) ? 2 : 3;
$values[] = $thisValue;
if ( count( $values ) == $lookingFor ) {
$number = ( $lookingFor == 3 ) ?
( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
$number = dechex($number);
$unicode[] = (strlen($number)==3)?"\u0".$number:"\u".$number;
$values = array();
$lookingFor = 1;
} // if
} // if
}
} // for
return implode("",$unicode);
}
当我传递像这样 ❤★♫Cute Pics 这样的字符串时,它应该给我这些符号的字符代码,为了实现这一点,我首先解码我的输入字符,然后将解码的字符串抛出给我的函数结果是这样的:\u2764\u2605\u266b Cute Pics,但是当我是 json_encode 时,这个数组中有界的字符串是:
$params[] = array(
'title' => $ad->utf8_to_unicode(html_entity_decode($_REQUEST["title$i"], ENT_COMPAT, "UTF-8")),
'description' => $ad->utf8_to_unicode(html_entity_decode($_REQUEST["desc$i"], ENT_COMPAT, "UTF-8")),
}
echo json_encode($params);
它在我的安卓手机无法解释的字符代码前添加了一个额外的斜线。由于这些字符代码被斜杠转义,但 json 再次放置了 android 手机无法读取的斜杠。另外,我没有使用数据库,所以我将这些 json 放在一个 json 格式的文件中,然后我使用 php 函数 file_get_content 从 json 文件中获取所有数据,然后使用它。请帮我解决这个问题。
【问题讨论】:
-
这段代码的目的是什么?你是想重写
json_encode()还是什么? -
本来要问同样的问题 - 你为什么要这样做?为什么不直接将 UTF 字符传递给 json_encode 并让它对其进行编码?
-
@Danack:如果我直接对输入进行json编码,那么它只显示json中的数字代码
-
如果我通过了 html_entity_decode($_REQUEST["title$i"], ENT_COMPAT, "UTF-8")) 参数,那么它会显示带有单斜杠的 json,但不能在移动设备上运行,是否存在有什么方法可以直接创建这些符号的字符代码?
-
"然后它显示带有单斜杠的 json,但不能在移动设备上工作" 它是如何不工作的?您看到会发生什么,您期望会发生什么?
标签: php arrays json unicode character-encoding