PHP取远程文件绝对路径

写采集程序的时候, 需要根据 当前页 和 下一页 计算出 下一页的绝对地址,写了一个常用的转换函数, 方便以后采集转换地址。

第一个参数是相对页面的url, 第二个参数是采集到的下一页地址, 然后就会返回一个带有http的下一个地址。

  1. <?php
  2. function get_abs_url($url, $filename) {
  3. // 如果是http|https|ftp|//开头的
  4. if (preg_match('/^[a-zA-Z]*\:?\/\//', $filename)) {
  5. return $filename;
  6. }
  7. // 如果是 /common/css/index.css
  8. if (substr($filename, 0, 1) == '/') {
  9. preg_match('/^[^\/]*\/\/[^\/]+/', $url, $domain);
  10. return $domain[0] . $filename;
  11. }
  12. // 去掉问号和#号后面的字符, 删除文件名, 只保留目录
  13. $url = strstr($url, '?', true) ? strstr($url, '?', true) :
  14. (strstr($url, '#', true) ? strstr($url, '#', true) : $url);
  15. $url = preg_replace('/[^\/]*$/', '', $url);
  16. // 如果是 ../../common/css/index.css
  17. if (substr($filename, 0, 3) == '../') {
  18. $filename = $url . $filename;
  19. while (preg_match('/[^\/\.]+\/\.\.\//', $filename)) {
  20. $filename = preg_replace('/[^\/\.]+\/\.\.\//', '', $filename);
  21. }
  22. return $filename;
  23. }
  24. // 如果是 ./common/css/index.css
  25. if (substr($filename, 0, 2) == './') {
  26. return str_replace('/./', '/', $url . $filename);
  27. }
  28. return $url . $filename;
  29. }

实现了下面几种模式:

  1. echo get_abs_url('http://www.baidu.com/blog/shuai/article/1.html?php=php#hello', '../../../common/css.css');
  2. echo '<br />';
  3. echo get_abs_url('http://www.baidu.com/blog/shuai/article/1.html?php=php#hello', 'http://static.baidu.com/common/css.css');
  4. echo '<br />';
  5. echo get_abs_url('http://www.baidu.com/blog/shuai/article/1.html?php=php#hello', '/common/css.css');
  6. echo '<br />';
  7. echo get_abs_url('http://www.baidu.com/blog/shuai/article/1.html?php=php#hello', './common/css.css');
  8. echo '<br />';
  9. echo get_abs_url('http://www.baidu.com/blog/shuai/article/1.html?php=php#hello', 'css.css');

sass安装

1. 下载和安装ruby

(1) 下载地址 http://rubyinstaller.org/downloads

(2) 安装过程中勾选Add Ruby Executables to your PATH
2015-06-19/5583b426820af

(3)启动Ruby命令行
2015-06-19/5583b4bf2e06a

2. 安装sass

(1) 删除默认的源
gem sources --remove https://rubygems.org/
(2) 指定淘宝的源
gem sources -a https://ruby.taobao.org/
(3) 执行下面的命令安装sass:
gem install sass

如果没有修改路径, 安装的sass路径在:
C:\Ruby22-x64\bin\sass.bat

到此安装结束

NetBeans 配置sass

(1) 菜单 -> 工具 -> 其他 -> css预处理程序 -> sass路径 填写C:\Ruby22-x64\bin\sass.bat
(2) 项目 -> 右击项目 -> 属性 -> css预处理程序 -> sass -> 添加输入和输出目录即可