如何转换我的PHP文件为utf8? [英] How to convert my php files to utf8?
问题描述
我正在开发基于自定义软键盘上的Android应用程序,并
包含不同的语言。
我如何把这些PHP文件为UTF8?
create_product.php
< HTML和GT;
< META HTTP-EQUIV =内容类型内容=text / html的;字符集= UTF-8/>
<身体GT;
< PHP/ *
*继code将创建一个新的产品行
*所有产品的详细信息,从HTTP POST请求读
* ///数组JSON响应
$响应=阵列();//检查必填字段
如果(使用isset($ _ POST ['名'])及和放大器;使用isset($ _ POST ['价格'])及和放大器;使用isset($ _ POST ['说明'])){ $名称= $ _ POST ['名'];
$价格= $ _ POST ['价格'];
$描述= $ _ POST ['说明']; //其中包括DB连接类
require_once __DIR__。 /db_connect.php'; //连接到数据库
$ DB =新DB_CONNECT(); // MySQL的插入新行
的mysql_query(SET NAMES UTF8);
$结果= mysql_query(INSERT INTO产品(名称,价格,描述)VALUES('$名称,$价格,$说明')); //检查是否插入行或不
如果($结果){
//成功插入到数据库
$响应[成功] = 1;
$响应[消息] =产品创建成功。 //呼应JSON响应
回声json_en code($响应);
}其他{
//没有插入一行
$响应[成功] = 0;
$响应[消息] =!哎呀发生错误。 //呼应JSON响应
回声json_en code($响应);
}
}其他{
//必填字段缺失
$响应[成功] = 0;
$响应[消息] =必填字段(s)是失踪; //呼应JSON响应
回声json_en code($响应);
}
?>
< /身体GT;
< / HTML>
get_all_products.php
< HTML和GT;
< META HTTP-EQUIV =内容类型内容=text / html的;字符集= UTF-8/>
<身体GT;
< PHP/ *
*继code将列出所有产品
* ///数组JSON响应
$响应=阵列();
//其中包括DB连接类
require_once __DIR__。 /db_connect.php';//连接到数据库
$ DB =新DB_CONNECT();//从产品表中的所有产品
的mysql_query(SET NAMES UTF8);
$结果= mysql_query(SELECT * FROM产品)或死亡(mysql_error());//检查空结果
如果(mysql_num_rows($结果)大于0){
//通过所有结果循环
//产品节点
$响应[产品] =阵列(); 而($行= mysql_fetch_array($结果)){
//临时用户阵
$产品=阵列();
$产品[PID] = $行[PID];
$产品[名称] = $行[名称];
$产品[价格] = $行[价格];
$产品[说明] = $行[说明];
$产品[created_at] = $行[created_at];
$产品[的updated_at] = $行[的updated_at]; //推单一产品到最终响应数组
array_push($响应[产品],$产品);
}
//成功
$响应[成功] = 1; //呼应JSON响应
回声json_en code($响应);
}其他{
//没有发现产品
$响应[成功] = 0;
$响应[消息] =没有找到产品; //回声没有用户JSON
回声json_en code($响应);
}
?>
< /身体GT;
< / HTML>
get_product_detail.php
< HTML和GT;
< META HTTP-EQUIV =内容类型内容=text / html的;字符集= UTF-8/>
<身体GT;
< PHP/ *
*继code将得到单品细节
*一个产品是由产品id标识(PID)
* ///数组JSON响应
$响应=阵列();
//其中包括DB连接类
require_once __DIR__。 /db_connect.php';//连接到数据库
$ DB =新DB_CONNECT();//检查数据后
的mysql_query(SET NAMES UTF8);
如果(使用isset($ _ GET [PID])){
$ PID = $ _GET ['PID']; //从产品表中的产品
$结果= mysql_query(SELECT * FROM产品WHERE PID = $ PID); 如果(!空($结果)){
//检查空结果
如果(mysql_num_rows($结果)大于0){ $结果= mysql_fetch_array($结果); $产品=阵列();
$产品[PID] = $结果[PID];
$产品[名称] = $结果[名称];
$产品[价格] = $结果[价格];
$产品[说明] = $结果[描述];
$产品[created_at] = $结果[created_at];
$产品[的updated_at] = $结果[的updated_at];
//成功
$响应[成功] = 1; //用户节点
$响应[产品] =阵列(); array_push($响应[产品],$产品); //呼应JSON响应
回声json_en code($响应);
}其他{
//没有产品找到
$响应[成功] = 0;
$响应[消息] =没有产品找到了; //回声没有用户JSON
回声json_en code($响应);
}
}其他{
//没有产品找到
$响应[成功] = 0;
$响应[消息] =没有产品找到了; //回声没有用户JSON
回声json_en code($响应);
}
}其他{
//必填字段缺失
$响应[成功] = 0;
$响应[消息] =必填字段(s)是失踪; //呼应JSON响应
回声json_en code($响应);
}
?>< /身体GT;< / HTML>
update_product.php
< HTML和GT;
< META HTTP-EQUIV =内容类型内容=text / html的;字符集= UTF-8/>
<身体GT;
< PHP/ *
*继code将更新产品信息
*一个产品是由产品id标识(PID)
* ///数组JSON响应
$响应=阵列();//检查必填字段
如果(使用isset($ _ POST ['PID'])及和放大器;使用isset($ _ POST ['名'])及和放大器;使用isset($ _ POST ['价格'])及和放大器;使用isset($ _ POST ['说明'])){ $ PID = $ _ POST ['PID'];
$名称= $ _ POST ['名'];
$价格= $ _ POST ['价格'];
$描述= $ _ POST ['说明']; //其中包括DB连接类
require_once __DIR__。 /db_connect.php'; //连接到数据库
$ DB =新DB_CONNECT(); 与匹配的PID // mysql的update行
$结果= mysql_query(更新产品集名称='$名称',价格= $价格,说明='$说明其中pid = $ PID); //检查是否插入行或不
如果($结果){
//成功更新
$响应[成功] = 1;
$响应[消息] =产品更新成功。 //呼应JSON响应
回声json_en code($响应);
}其他{ }
}其他{
//必填字段缺失
$响应[成功] = 0;
$响应[消息] =必填字段(s)是失踪; //呼应JSON响应
回声json_en code($响应);
}
?>
< /身体GT;< / HTML>
delete_product.php
< HTML和GT;
< META HTTP-EQUIV =内容类型内容=text / html的;字符集= UTF-8/>
<身体GT;
< PHP/ *
*继code将删除表中的产品
*一个产品是由产品id标识(PID)
* ///数组JSON响应
$响应=阵列();//检查必填字段
如果(使用isset($ _ POST ['PID'])){
$ PID = $ _ POST ['PID']; //其中包括DB连接类
require_once __DIR__。 /db_connect.php'; //连接到数据库
$ DB =新DB_CONNECT(); 与匹配的PID // mysql的update行
$结果= mysql_query(DELETE FROM产品WHERE PID = $ PID); //检查是否行删除或不
如果(mysql_affected_rows()函数大于0){
//成功更新
$响应[成功] = 1;
$响应[消息] =产品成功删除; //呼应JSON响应
回声json_en code($响应);
}其他{
//没有产品找到
$响应[成功] = 0;
$响应[消息] =没有产品找到了; //回声没有用户JSON
回声json_en code($响应);
}
}其他{
//必填字段缺失
$响应[成功] = 0;
$响应[消息] =必填字段(s)是失踪; //呼应JSON响应
回声json_en code($响应);
}
?>< /身体GT;< / HTML>
JSONparser.java
进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口java.io.UnsupportedEncodingException;
进口的java.util.List;进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.client.utils.URLEn codedUtils;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.util.Log;公共类JSONParser { 静态InputStream为= NULL;
静态的JSONObject jObj = NULL;
静态JSON字符串=; //构造
公共JSONParser(){ } //函数得到JSON的网址
//通过HTTP POST或GET mehtod
公众的JSONObject makeHtt prequest(URL字符串,字符串的方法,
清单<&的NameValuePair GT; PARAMS){ //使HTTP请求
尝试{ //检查请求的方法
如果(方法==POST){
//请求方法是POST
// defaultHttpClient
DefaultHttpClient的HttpClient =新DefaultHttpClient();
HttpPost httpPost =新HttpPost(URL);
httpPost.setEntity(新UrlEn codedFormEntity(PARAMS)); HTT presponse HTT presponse = httpClient.execute(httpPost);
HttpEntity httpEntity = HTT presponse.getEntity();
是= httpEntity.getContent(); }否则如果(方法==GET){
//请求方法是GET
DefaultHttpClient的HttpClient =新DefaultHttpClient();
字符串中的paramString = URLEn codedUtils.format(参数,可以UTF-8);
网址+ =? +中的paramString;
HTTPGET HTTPGET =新HTTPGET(URL); HTT presponse HTT presponse = httpClient.execute(HTTPGET);
HttpEntity httpEntity = HTT presponse.getEntity();
是= httpEntity.getContent();
}
}赶上(UnsupportedEncodingException五){
e.printStackTrace();
}赶上(ClientProtocolException E){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
} 尝试{
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
是,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\\ n);
}
is.close();
JSON = sb.toString();
}赶上(例外五){
Log.e(缓冲区错误,错误转换结果+ e.toString());
} //尝试分析字符串到一个JSON对象
尝试{
jObj =新的JSONObject(JSON);
}赶上(JSONException E){
Log.e(JSON解析器,错误分析数据+ e.toString());
} //返回JSON字符串
返回jObj; }
}
那么,如果它是一个编码错误像你说的,你可以用记事本+ +(其中,如果你没有它,得到它)。在该文件中的设置,你会去:
编码>恩code为UTF-8(无BOM)在页面的顶部。
这是我所用,所以如果编码错误,这会解决它。
I am developing an Android app which is based on a custom soft keyboard and contains different languages.
How do I convert these php files to utf8?
create_product.php
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<body>
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
mysql_query("SET NAMES UTF8");
$result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
</body>
</html>
get_all_products.php
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<body>
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
mysql_query("SET NAMES UTF8");
$result = mysql_query("SELECT *FROM products") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["pid"] = $row["pid"];
$product["name"] = $row["name"];
$product["price"] = $row["price"];
$product["description"] = $row["description"];
$product["created_at"] = $row["created_at"];
$product["updated_at"] = $row["updated_at"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
</body>
</html>
get_product_detail.php
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<body>
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
mysql_query("SET NAMES UTF8");
if (isset($_GET["pid"])) {
$pid = $_GET['pid'];
// get a product from products table
$result = mysql_query("SELECT *FROM products WHERE pid = $pid");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["pid"] = $result["pid"];
$product["name"] = $result["name"];
$product["price"] = $result["price"];
$product["description"] = $result["description"];
$product["created_at"] = $result["created_at"];
$product["updated_at"] = $result["updated_at"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?></body></html>
update_product.php
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<body>
<?php
/*
* Following code will update a product information
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['pid']) && isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {
$pid = $_POST['pid'];
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql update row with matched pid
$result = mysql_query("UPDATE products SET name = '$name', price = '$price', description = '$description' WHERE pid = $pid");
// check if row inserted or not
if ($result) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Product successfully updated.";
// echoing JSON response
echo json_encode($response);
} else {
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
</body></html>
delete_product.php
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<body>
<?php
/*
* Following code will delete a product from table
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql update row with matched pid
$result = mysql_query("DELETE FROM products WHERE pid = $pid");
// check if row deleted or not
if (mysql_affected_rows() > 0) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Product successfully deleted";
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?></body></html>
JSONparser.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Well if it is an encoding error like you say, you can use Notepad++ (which if you don't have it, get it). In the settings of the file, you would go to:
Encoding > Encode in UTF-8 (without BOM) at the top of the page.
This is what I use, so if it is an encoding error, this would fix it.
这篇关于如何转换我的PHP文件为utf8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!