PHP
[PHP]微博用分割消息代码(避免分割URL等语素
给自己的推特机器人用的代码,避免分割URL、@、#,另外会在后面表明消息总条数和当前编号,另外由于多数客户端都是新的在前面,所以逆向输出以方便阅读
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | public function tweet($msg) { $msg = trim($msg); if(($len = mb_strlen($msg))>130) { if($len > 1200) gx::throw_('tooooo long msg x_x'); $nonbreak = '%((?:@[a-zA-Z0-9_\-]+)|(?:#[\S]+)|(?:https?://[\w\s\.]+(?:/\S*)?))%'; $units = preg_split($nonbreak, $msg,-1, PREG_SPLIT_DELIM_CAPTURE); $msgs = array(); $buffer = ''; while($u=array_shift($units)) { if(mb_strlen($buffer)+mb_strlen($u) > 120) { if(preg_match($nonbreak, $u)) {//this unit is non-break! $msgs[] = $buffer; $buffer = $u; continue; } //break words $buffer .= $u; while(mb_strlen($buffer)>120) { $msgs[] = mb_substr($buffer,0,120); $buffer = mb_substr($buffer,120); } } else {//not too long yet, wait for next unit $buffer .= $u; } } $now = $all = count($msgs); $objs = array(); while($m = array_pop($msgs)) {//output in reversed order $objs[] = $this->tweet($m."($now/$all)"); $now--; } return $objs; } list($code, $response) = $this->request('1/statuses/update.json', 'POST', array( 'status' => $msg )); if ($code == 200) { return json_decode($response); } else { gx::throw_(htmlentities($response), $code); } } |
[PHP]QueryPath中的编码问题
其实发现QueryPath这个神奇的类库已经是几年前的事情了,QueryPath是一个PHP的DOM操作类库,仿jQuery风格。最近终于有机会来实战一把,却遇到了万恶的编码问题,根据API文档用convert_to_encoding来控制编码却一直失败(源码是UTF-8的),拿起了传说中的霰弹枪一顿乱打之后,还是在PHP官网的手册评论里面找到了答案,嗯,来一段代码或许更清楚一些
8adeb86[......]
thinkPHP中的一个函数require_cache的性能问题研究
thinkPHP中有个require_cache函数包装了require,显示地实现了@require_once的功能,注释中标榜为“优化的require_once”,然而实际情况如何,我做了个很简单的功能测试,for循环1000000次分别测试require_cache和require_once的性能
require_once: 2.009112, 2.099082, 2.075355
require_cache:23.285378, 23.309440, 23.334591
require_test:2.745165, 2.731406, 2.859381
require_test2:23.386466, 23.116133, 23.034672
结果很明显了,是由于每次的realpath调用严重增加了IO开销导致的性能急剧下降,而其他因素如函数调用、静态变量使用等对性能影响相对于IO开销都可以忽略不计,毕竟是在内存发生的,顺带一提,测试环境是win7+APMServ+ThinkPHP2.0
当然,对于实际应用来说,由于不会有如此大数量的require发生,所以十倍的性能差距基本可以无视,但ThinkPHP的这个函数仍然摆脱不了画蛇添足的嫌疑
下面:附录1&2,require_cache代码以及测试用代码
[......]
[PHP]简单的批量复制UE工程中的文件
习惯用UE来敲PHP代码,刚才试用了一下一直有的“工程”功能,发现虽然CTAGS对PHP没用,但因为工程文件是很和谐的INI文件格式,所以除了方便打开相关文件以外,也能很方便地写脚本导出相关文件,下面给出PHP版本,在头部写上工程文件的绝对路径和要导出到的目录,运行即可
点继续阅读看源码……
[......]
今天的PHP学习笔记:关于双美元$$
有很多时候会觉得很方便的双刀语法$$var在指向array的时候表现得和预期有所不同( @ PHP5.2.6,未测试其它版本)
下面是测试代码,注释掉的那行会导致Fatal错误
(CodeBox在首页不工作,详情请点继续阅读)[......]
高级PHP应用程序漏洞审核技术
全文转载 http://www.ph4nt0m.org-a.googlepages.com/PSTZine_0x03_0x06.txt
==Ph4nt0m Security Team==
Issue 0×03, Phile #0×06 of 0×07
|=————-[......]
标签
McFog’s Bot
- mcfog_bot: 主人@mcfog 在#Bangumi 看过 ep.4 Put flesh -輪郭-(http://t.co/Wuq91wut) Another(http://t.co/FTrqNeIo)
- mcfog_bot: 主人@mcfog 在#Bangumi 看过 ep.5 恋愛禁止令(http://t.co/kwUehkE6) アクエリオンEVOL(http://t.co/fn60m9QW)
- mcfog_bot: 主人@mcfog 在#Bangumi 看过 ep.4 部長の選択(http://t.co/lvffUIpo) 新テニスの王子様(http://t.co/5mQVa1mf)
- mcfog_bot: 主人@mcfog 在#Bangumi 将 Meteora(http://t.co/7mFpW1z6) 加为了好友
- mcfog_bot: 主人@mcfog 在#Bangumi 看过 ep.4 女王陛下の恩賞(http://t.co/rhzuIqfn) ゼロの使い魔F(http://t.co/LUx1jwx9)