• Welcome to Rotar E@rth!
  • Feel free to look around and comment~
  • Tin - for a better fantasy life!
  • TinTimer - for an even better fantasy life!

PMangar再启动计划 | Revival of PMangar

Blog Rotar 12年前 (2012-05-31) 2679次浏览 0个评论

大概是从去年开始着手制作了一个简陋的看漫画用的php网站PMangar,功能只有通过股沟在178上面搜索与输入字符相关的漫画,然后一页一张图的方式进行浏览,当时在Kindle和手机上测试效果都还凑合,于是就收工了。近来无事,于是又测试了一下,发觉功能基本失效,于是重新检查和改进,总算是又能恢复使用了。传送门在此:http://rotar.tk/pm

本文便用来记录复活PMangar过程中的领悟和心得,与诸君共勉。

————————–Lang Sep—————————–

PMangar was an simple php application that fetches comics from 178.com and display it to the user. In other words, its just an online comic viewer. The work are considered done in last year as the result of using it on both Kindle and my Nokia phone seems to be satisfactory. However, when I checked the application few days ago, nothing seems to be working and therefore I’m here to repair and upgrade it. The currently working version is available at http://rotar.tk/pm.

This post intend to share what I’ve learnt about php during the process and hope it may help.

1. preg_match(_all)( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )

此前对这个函数并没有深入了解过,于是在提取固定位置的字符串的时候用的是先找前后固定字符然后把中间提出来然后利用固定的起始位置将所需要的文字切出来的办法,不可谓不麻烦。而看过preg_match的example之后,发现只需要在 $pattern 中把所需要的文字部分用括号括住就可以在 $matches[1] 中直接获得需要的内容。

例子如下 Example:

<?php
$string = "href=\"http://www.xxxx.com/yyy.html\"> <a style=\"color:red\">测试用文字 Test text</a>";
$pattern = "/<a[^>]*>([\x{4e00}-\x{9fff} \w]*)<\/a>/u";

preg_match($pattern, $string, $matches);
echo($matches[0]."\n");
echo($matches[1]."\n");
?>

Result:
<a style="color:red">测试用文字 Test text</a>
测试用文字 Test text

也可以使用多个括号,用于提取多处需要匹配的字符。
值得一提的是这里使用了Unicode的范围来匹配汉字,4e00-9fff是根据http://unicode-table.com上的字符集范围资料确定的。


本网站采用BY-NC-SA协议进行授权 , 转载请注明原文链接:PMangar再启动计划 | Revival of PMangar
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址