You must say:"Yes" or "All right"! 注册 | 登陆
浏览模式: 标准 | 列表2009年07月的文章

PHP与正则表达式中的模式修正符

正则表达式的匹配先后顺序:
1.模式单元
2.重复匹配 ? * + {}
3.边界限定 ^ $ b B
4.模式选择 |
模式修正符:
模式修正符是标记在整个模式之外的.
i :模式中的字符将同时匹配大小写字母.
m :字符串视为多行.
s :将字符串视为单行,换行符作为普通字符.
x :将模式中的空白忽略.
A :强制仅从目标字符串的开头开始匹配.
D :模式中的美元元字符仅匹配目标字符串的结尾.
U :匹配最近的字符串.

» 阅读全文

Tags: php, 正则

PHP采集实例,支持正则设置开始结束区域

PHP代码
  1. <?php      
  2. function preg_substr($start$end$str// 正则截取函数      
  3. {      
  4.     $temp = preg_split($start$str);      
  5.     $content = preg_split($end$temp[1]);      
  6.     return $content[0];      
  7. }   
  8. function str_substr($start$end$str// 字符串截取函数      
  9. {      
  10.     $temp = explode($start$str, 2);      
  11.     $content = explode($end$temp[1], 2);      
  12.     return $content[0];      
  13. }   
  14. // ---------------- 使用实例 ----------------   
  15. $str = iconv("UTF-8""GB2312"file_get_contents("http://www.kalvin.cn/?action=show&id=197"));    
  16. echo ('标题: ' . str_substr("<title>""</title>"$str)); // 通过字符串提取标题   
  17. echo ('作者: ' . preg_substr("/userid=\d+\">/""/<\//"$str)); // 通过正则提取作者   
  18. echo ('内容: ' . str_substr('<div class="content">''</div>'$str)); //内容当然不可以少   
  19. ?>  

Tags: php, 正则

通过php file_get_contents函数取得cookie值及提交或获取数据

PHP代码
  1. <?php   
  2.   
  3. function dologin ($url$postdata$referer// post获取cookie值用   
  4. {   
  5.     $optionlogin = array('http' => array('method' => "POST"'header' => "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.0.04506; CIBA)\r\nAccept:*/*\r\nReferer:" . $referer,   
  6.             'content' => $postdata,));   
  7.     $file = file_get_contents($url, false , stream_context_create($optionlogin));   
  8.     preg_match_all("/Set-Cookie:(.*?)\r\n/", implode("\r\n"$http_response_header), $cookies);   
  9.     session_start();   
  10.     $_SESSION["doCookie"] = implode(";"$cookies[1]);   
  11.     return $file;   
  12. }    
  13.   
  14. function dopost ($url$postdata$referer$cookie// post提交数据使用   
  15. {   
  16.     $optionpost = array('http' => array('method' => "POST"'header' => "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.0.04506; CIBA)\r\nAccept:*/*\r\nReferer:" . $referer . "\r\nCookie:" . $cookie'content' => $postdata,));   
  17.     $file = file_get_contents($url, false , stream_context_create($optionpost));   
  18.     return $file;   
  19. }    
  20.   
  21. function doget ($url$referer$cookie// get获取数据使用   
  22. {   
  23.     $optionget = array('http' => array('method' => "GET"'header' => "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.0.04506; CIBA)\r\nAccept:*/*\r\nReferer:" . $referer . "\r\nCookie:" . $cookie));   
  24.     $file = file_get_contents($url, false , stream_context_create($optionget));   
  25.     return $file;   
  26. }    
  27.   
  28. ?>   

百分百原创,欢迎拍砖!需要PHP5.0以上版本才可以使用以上函数!

Tags: php

兼容Firefox的加入收藏链接代码

XML/HTML代码
  1. <script type="text/javascript">  
  2. <!--   
  3. function addFavorite() {   
  4.     var url = this.location.href;   
  5.     var title = this.document.title;   
  6.     if (document.all) {   
  7.         window.external.addFavorite(url,title);   
  8.     }   
  9.     else if (window.sidebar) {   
  10.         window.sidebar.addPanel(title, url, "");   
  11.     }   
  12.     else {   
  13.         alert("暂时不支持您使用的浏览器版本");   
  14.     }   
  15.     return false;   
  16. }      
  17. //-->  
  18. </script>  
  19. <a href="#" onclick="return addFavorite();" target="_top" rel="sidebar">加入收藏夹</a>  

以上代码可以通用IE及Firefox,但是不支持Chrome及Opera,不过当不支持某种浏览器时,会弹出提示!原来有发布一个简洁的加入收藏夹代码,兼容ie,firefox,opera等浏览器,是一个不错的选择!

Tags: html

让PHP File函数支持自定义Header

 

PHP代码
  1. <?php       
  2. $option = array(       
  3.     'http' => array(       
  4.         'method' => "POST"// 常用 POST 或者 GET       
  5.         'header' => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: */*"// Header 域内容,用于定义如 Cookie 之类的信息       
  6.         'content' => "domain=www.kalvin.cn&author=kalvin"// POST 时提交的内容       
  7.         )       
  8.     );       
  9. $xoption = stream_context_create($option); // 生成请求所用的头信息       
  10. echo $str = file_get_contents("http://www.kalvin.cn", false, $xoption); // 执行请求   
  11. print_r($http_response_header); // 显示返回的头信息   
  12. ?>  

File_get_contents 的 POST 方式需要 PHP5 以后才支持,孤魂原来还是习惯使用 Curl ,不过有的虚拟主机没有打开 Curl 扩展的情况下,就可以考虑使用到以上代码了。另外今天还看到了一个 http_build_query() 函数,以后整理火车头发布模块的数据包时,效率提高不少!

Tags: php, curl

Records:912