将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);
} 通过http推送消息给socket,socket服务再向客户端推送<?php /* * Socket推送 * 请用守护进程方式启动php msgservice.php & (socket只...
001源码:/* * $xml_str是xml字符串 */ function xmltoarray($xml_str) { //禁止XML实体扩展攻击 libxml_disable_entity_loader(true); //拒绝包含...
使用php函数array_multisort()即可实现和SQL一样的order by排序. 例如我们需要对会员表按照主键降序排列,年龄升序排列://会员表数据 $list = []; $list[] = ['mid' =>&n...
逛公众号文章看到文章"php实现事件监听与触发的方法,你用过吗?",我就好奇了,php又不是asp.net的webform,哪里来的服务端事件监听。于是学习了一波。先看下监听类:class Event { /** &nbs...
ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...
Redis提供了发布订阅功能,可以用于消息的传输,Redis的发布订阅机制包括三个部分,发布者(publisher),订阅者(subscriber)和频道(channel)。 发布者和订阅者都是Redis客户端,Channel则为Redis服务器端,发布者将消息发送到某个的频道,订阅了这个...