实现代码如下:
<?php /** * php curl采集Discuz * www. */ set_time_limit(0); //cookie保存目录 $cookdir = './cookie.tmp'; //模拟请求数据 Function request($url,$action,$cookdir,$referer){ $ch = curl_init(); $options = array(CURLOPT_URL => $url, CURLOPT_HEADER => 0, CURLOPT_NOBODY => 0, CURLOPT_PORT => 80, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $action, CURLOPT_RETURNTRANSFER => 1, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_COOKIEJAR => $cookdir, CURLOPT_COOKIEFILE => $cookdir, CURLOPT_REFERER => $referer ); curl_setopt_array($ch, $options); $code = curl_exec($ch); curl_close($ch); return $code; } //获取帖子列表 Function getList($code){ //<a href="/blog_article/viewthread/tid/16/amp;extra/page/1.html">加打塔洗花腳本</a> preg_match_all('/<a href=/index.html"viewthread.php\?tid=(\d+)/',$code,$threads); return $threads[1]; } //判断该帖子是否存在 Function isExits($code){ preg_match('/<p>指定的主题不存在或已被删除或正在被审核,请返回。<\/p>/',$code,$error); return isset()($error[0])?false:true; } //获取帖子标题 Function getTitle($code){ preg_match('/<h1>[^<\/h1>]*/',$code,$title_tmp); $title = $title_tmp[0]; return $title; } //登录论坛/ $url = 'http://www./logging.php?action=login'; $action='/blog_article/loginfield=username&username=see7di&password=fjin999&questionid=0&cookietime=315360000&referer=http_/bbs.war3.cn/&loginsubmit=提交/index.html'; request($url,$action,$cookdir,''); unset($action,$url); //获取帖子列表(位于第一页的帖子) $url = 'http://www./forumdisplay.php?fid=5'; $code = request($url,'',$cookdir,''); $tList = getList($code); //循环抓取每個帖子內的標題 foreach($tList as $list){ $url = "http://www./viewthread.php?tid={$list}"; $code = request($url,'',$cookdir,''); if(isExits($code)){ $title = getTitle($code); echo "tid:{$list}:",strip_tags()($title),"<br>"; }else{ echo "tid:{$list}:该帖子不存在!<br>"; } } ?>
数据类似下面这样:
--------------------------------
1| ,4,5,7,
--------------------------------
3| ,4,5,8,
--------------------------------
4| ,5,4,
--------------------------------
6| ,5,4,7,8,
--------------------------------
7| ,8,4,5,7,9,
--------------------------------
8| ,7,9,4,5,8,10,
假如我post过来的cid值分别是4和5,那我如何精确的查询到id=4的这条数据?
同理,假如我post过来的cid值分别是4,5,7,8的时候,那我如何精确的查询到id=6的这条数据?
注意:
cid这一列的数据排列是非固定式的,没规律可循,唯一能够确认的就是不会重复出现同一个值
还有就是用like写法and链接这种方式我试过了,不够精确.例如...where cid like'%,4,%' and cid like'%,5,%'
解决方法:
分别用了三种方法做测试,测试的时候是循环20000次提取数据库里的数据,结果如下:
//1770 ms~1800 ms
Select id,type From hotel where type like'%|21|%'and type like'%|101|%'and LENGTH(type)=length('|21|101|')
//1760 ms~1810 ms
Select id,type From hotel where locate('|21|',type)>0and locate('|101|',type)>0and LENGTH(type)=length('|21|101|')
//CPU直接100%,瀏覽器挂掉
Select id,type From hotel where type REGEXP concat('.*[',replace('21,101',',','|'),'].*')
php实现模糊而又精确的查找就这么实现了,是不是很有意思呢?
以上就是今天php 教程的内容,希望本文给出的方法与思路,对大家有所帮助。
最常见的词语二分法:
<?php $str = '这是我的网站www.!'; //$str = iconv('GB2312','UTF-8',$str); $result = spStr($str); print_r($result); /** * UTF-8版 中文二元分词 */ function spStr($str) { $cstr = array(); $search = array(",", "/", "\\", ".", ";", ":", "\"", "!", "~", "`", "^", "(", ")", "?", "-", "\t", "\n", "'", "<", ">", "\r", "\r\n", "{1}quot;", "&", "%", "#", "@", "+", "=", "{", "}", "[", "]", ":", ")", "(", ".", "。", ",", "!", ";", "“", "”", "‘", "’", "[", "]", "、", "—", " ", "《", "》", "-", "…", "【", "】",); $str = str_replace()($search, " ", $str); preg_match_all("/[a-zA-Z]+/", $str, $estr); preg_match_all("/[0-9]+/", $str, $nstr); $str = preg_replace("/[0-9a-zA-Z]+/", " ", $str); $str = preg_replace("/\s{2,}/", " ", $str); $str = explode()(" ", trim($str)); foreach ($str as $s) { $l = strlen($s); $bf = null; for ($i= 0; $i< $l; $i=$i+3) { $ns1 = $s{$i}.$s{$i+1}.$s{$i+2}; if (isset()($s{$i+3})) { $ns2 = $s{$i+3}.$s{$i+4}.$s{$i+5}; if (preg_match("/[\x80-\xff]{3}/",$ns2)) $cstr[] = $ns1.$ns2; } else if ($i == 0) { $cstr[] = $ns1; } } } $estr = isset($estr[0])?$estr[0]:array(); $nstr = isset($nstr[0])?$nstr[0]:array(); return array_merge($nstr,$estr,$cstr); } ?>
結果是:
Array ( [0] => www [1] => xxx [2] => net [3] => 这是 [4] => 是我 [5] => 我的 [6] => 的网 [7] => 网站 )
如果以上结果转换为区位码,则如下代码所示:
<?php foreach ($result as $s) { $s = iconv('UTF-8','GB2312',$s); $code[] = gbCode($s); } $code = implode(" ", $code); echo $code; function gbCode($str) { $return = null; if (!preg_match("/^[\x80-\xff]{2,}$/",$str)) return $str; $len = strlen($str); for ($i= 0; $i< $len; $i=$i+2) { $return .= sprintf()("%02d%02d",ord($str{$i})-160,ord($str{$i+1})-160); } return $return; } ?>