将jsonp转为PHP数组和对象。
/**
* jsonp转数组|Jsonp转json
* @param string $jsonp jsonp字符串
* @param bool $assoc true转数组 false转对象
* @return array|ArrayObject|null
*/
function jsonp_decode($jsonp, $assoc = false)
{
$pattern = '/\((.*)\)/s';
if (preg_match($pattern, $jsonp, $matches))
{
if (!empty($matches['1']))
{
return json_decode($matches['1'], $assoc);
}
return null;
}
return null;
}使用实例:
//jsonp字符串 $jsonp = 'callbackFunction(["customername1","customername2"])'; //jsonp转数组 $jsonp_result = jsonp_decode($jsonp,true); var_dump($jsonp_result);
转换结果:
array(2) {
[0]=>
string(13) "customername1"
[1]=>
string(13) "customername2"
}也可以试试下面的这个方法,宇润写的,我没有测试:
/**
* 把jsonp转为php数组
* @param string $jsonp jsonp字符串
* @param boolean $assoc 当该参数为true时,将返回array而非object
* @return array
*/
function jsonp_decode($jsonp, $assoc = false)
{
$jsonp = trim($jsonp);
if(isset($jsonp[0]) && $jsonp[0] !== '[' && $jsonp[0] !== '{') {
$begin = strpos($jsonp, '(');
if(false !== $begin)
{
$end = strrpos($jsonp, ')');
if(false !== $end)
{
$jsonp = substr($jsonp, $begin + 1, $end - $begin - 1);
}
}
}
return json_decode($jsonp, $assoc);
} 项目需要使用websocket推送最新订单,客户服务器非linux不支持swoole,因此使用原生,直接上代码(1).PHP服务端<?php ini_set('error_reporting', E_ALL ^ E_NOTICE); ini_set...
首先看看以下代码:代码1:<?php $a=0.1; $b=0.7; if($a+$b==0.8) { echo "1"; } else{ echo "2"; } ?>代码2:<?php &n...
php官方已经提供了Iterator(迭代器)接口,通过网上资料的学习,目前看适合超大集合或者数组提取使用。学习一个函数的实现对比内存占用差距.使用迭代器和普通循环实现range()函数。(1).普通循环实现range()函数。function newrange($low, $h...
逛公众号文章看到文章"php实现事件监听与触发的方法,你用过吗?",我就好奇了,php又不是asp.net的webform,哪里来的服务端事件监听。于是学习了一波。先看下监听类:class Event { /** &nbs...
上篇文章已经讲解arrayacces的原理,现在来讲解下arrayaccess的实际应用。一个大型的互联网项目中必然会存在各种配置信息,例如多种数据库信息:mysql,tidb,mongodb,redis,某个业务模块单独的配置信息如比例,额度等等,那么该如何治理配置信息?PHP项目中大部分的框架都...
Redis提供了发布订阅功能,可以用于消息的传输,Redis的发布订阅机制包括三个部分,发布者(publisher),订阅者(subscriber)和频道(channel)。 发布者和订阅者都是Redis客户端,Channel则为Redis服务器端,发布者将消息发送到某个的频道,订阅了这个...