PHP通过url传递字符串编码数据
i use PHP file to send data from my web to JS. And i sending data to php by URL. For xample. www.web.com/send.php?data=Hello everybody. And my problem is, if someone send special character or emoticon, PHP script wont work. Is there any function to encode string to string only with letters and numbers, and on PHP side decode it? Thanks, for replyes ;).
我使用PHP文件将数据从我的网页发送到JS。 我通过URL将数据发送到php。 对于xample。 www.web.com/send.php?data=Hello所有人。 而我的问题是,如果有人发送特殊字符或表情符号,PHP脚本将无法正常工作。 是否有任何函数只能用字母和数字将字符串编码为字符串,在PHP端解码它? 谢谢,回复;)。 p> div>
In javascript use:
encodeURIComponent('Hello everybody')
In PHP use:
$data=$_GET['data'];
In javascript you should use the following function to encode the url:
var url = 'www.web.com/send.php?data='+encodeURIComponent('Hello everybody');
in php you can use:
$data = $_GET['data'];
I'am using these functions for decode/encode
public function encode($string)
{
$string=$string*1498158+825920496;
return urlencode(base64_encode($string));
}
///////////////////////////////////////////////////////////////////////////////
public function decode($string)
{
$string=base64_decode(urldecode($string));
$string=($string-825920496)/1498158;
return $string;
}