PHP 采集程序 常用函数


Posted in PHP onDecember 18, 2008

当前的脚本网址
function get_php_url(){
if(!empty($_SERVER["REQUEST_URI"])){
$scriptName = $_SERVER["REQUEST_URI"];
$nowurl = $scriptName;
}else{
$scriptName = $_SERVER["PHP_SELF"];
if(empty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;
else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
}
return $nowurl;
}
//把全角数字转为半角数字
function GetAlabNum($fnum){
$nums = array("0","1","2","3","4","5","6","7","8","9");
$fnums = "0123456789";
for($i=0;$i<=9;$i++) $fnum = str_replace($nums[$i],$fnums[$i],$fnum);
$fnum = ereg_replace("[^0-9\.]|^0{1,}","",$fnum);
if($fnum=="") $fnum=0;
return $fnum;
}
//去除HTML标记
function Text2Html($txt){
$txt = str_replace(" "," ",$txt);
$txt = str_replace("<","<",$txt);
$txt = str_replace(">",">",$txt);
$txt = preg_replace("/[\r\n]{1,}/isU","<br/>\r\n",$txt);
return $txt;
}
//清除HTML标记
function ClearHtml($str){
$str = str_replace('<','<',$str);
$str = str_replace('>','>',$str);
return $str;
}
//相对路径转化成绝对路径
function relative_to_absolute($content, $feed_url) {
preg_match('/(http|https|ftp):\/\//', $feed_url, $protocol);
$server_url = preg_replace("/(http|https|ftp|news):\/\//", "", $feed_url);
$server_url = preg_replace("/\/.*/", "", $server_url);
if ($server_url == '') {
return $content;
}
if (isset($protocol[0])) {
$new_content = preg_replace('/href="\//', 'href="'.$protocol[0].$server_url.'/', $content);
$new_content = preg_replace('/src="\//', 'src="'.$protocol[0].$server_url.'/', $new_content);
} else {
$new_content = $content;
}
return $new_content;
}
//取得所有链接
function get_all_url($code){
preg_match_all('/<a\s+href=["|\']?([^>"\' ]+)["|\']?\s*[^>]*>([^>]+)<\/a>/i',$code,$arr);
return array('name'=>$arr[2],'url'=>$arr[1]);
}
//获取指定标记中的内容
function get_tag_data($str, $start, $end){
if ( $start == '' || $end == '' ){
return;
}
$str = explode($start, $str);
$str = explode($end, $str[1]);
return $str[0];
}
//HTML表格的每行转为CSV格式数组
function get_tr_array($table) {
$table = preg_replace("'<td[^>]*?>'si",'"',$table);
$table = str_replace("</td>",'",',$table);
$table = str_replace("</tr>","{tr}",$table);
//去掉 HTML 标记
$table = preg_replace("'<[\/\!]*?[^<>]*?>'si","",$table);
//去掉空白字符
$table = preg_replace("'([\r\n])[\s]+'","",$table);
$table = str_replace(" ","",$table);
$table = str_replace(" ","",$table);
$table = explode(",{tr}",$table);
array_pop($table);
return $table;
}
//将HTML表格的每行每列转为数组,采集表格数据
function get_td_array($table) {
$table = preg_replace("'<table[^>]*?>'si","",$table);
$table = preg_replace("'<tr[^>]*?>'si","",$table);
$table = preg_replace("'<td[^>]*?>'si","",$table);
$table = str_replace("</tr>","{tr}",$table);
$table = str_replace("</td>","{td}",$table);
//去掉 HTML 标记
$table = preg_replace("'<[\/\!]*?[^<>]*?>'si","",$table);
//去掉空白字符
$table = preg_replace("'([\r\n])[\s]+'","",$table);
$table = str_replace(" ","",$table);
$table = str_replace(" ","",$table);
$table = explode('{tr}', $table);
array_pop($table);
foreach ($table as $key=>$tr) {
$td = explode('{td}', $tr);
array_pop($td);
$td_array[] = $td;
}
return $td_array;
}
//返回字符串中的所有单词 $distinct=true 去除重复
function split_en_str($str,$distinct=true) {
preg_match_all('/([a-zA-Z]+)/',$str,$match);
if ($distinct == true) {
$match[1] = array_unique($match[1]);
}
sort($match[1]);
return $match[1];
}

PHP 相关文章推荐
php中将字符串转为HTML的实体引用的一个类
Feb 03 PHP
解析PHP中的file_get_contents获取远程页面乱码的问题
Jun 25 PHP
php上传图片到指定位置路径保存到数据库的具体实现
Dec 30 PHP
PHP使用CURL获取302跳转后的地址实例
May 04 PHP
php实现xml与json之间的相互转换功能实例
Jul 07 PHP
注释PHP和html混合代码的小技巧(分享)
Nov 03 PHP
PHP会话控制实例分析
Dec 24 PHP
php自定义函数实现统计中文字符串长度的方法小结
Apr 15 PHP
php微信公众号开发之微信企业付款给个人
Oct 04 PHP
PHP析构函数destruct与垃圾回收机制的讲解
Mar 22 PHP
laravel 输出最后执行sql 附:whereIn的使用方法
Oct 10 PHP
php优化查询foreach代码实例讲解
Mar 24 PHP
php IP及IP段进行访问限制的代码
Dec 17 #PHP
php mssql 数据库分页SQL语句
Dec 16 #PHP
linux下 C语言对 php 扩展
Dec 14 #PHP
php 文件状态缓存带来的问题
Dec 14 #PHP
快速开发一个PHP扩展图文教程
Dec 12 #PHP
PHP Google的translate API代码
Dec 10 #PHP
php escape URL编码
Dec 10 #PHP
You might like
javascript与webservice的通信实现代码
2010/12/25 Javascript
jquery插件实现鼠标经过图片右侧显示大图的效果(类似淘宝)
2013/02/04 Javascript
JQuery中form验证出错信息的查看方法
2013/10/08 Javascript
jQuery jcrop插件截图使用方法
2013/11/20 Javascript
jquery操作对象数组元素方法详解
2014/11/26 Javascript
jQuery搜索框效果实现代码(百度关键词联想)
2021/02/25 Javascript
微信小程序 wxapp内容组件 progress详细介绍
2016/10/31 Javascript
js获取浏览器高度 窗口高度 元素尺寸 偏移属性的方法
2016/11/21 Javascript
Vue.js双向绑定实现原理详解
2016/12/22 Javascript
原生js实现电商侧边导航效果
2017/01/19 Javascript
扩展bootstrap的modal模态框-动态添加modal框-弹出多个modal框
2017/02/21 Javascript
BootStrap Table复选框默认选中功能的实现代码(从数据库获取到对应的状态进行判断是否为选中状态)
2017/07/11 Javascript
vue 里面使用axios 和封装的示例代码
2017/09/01 Javascript
node跨域转发 express+http-proxy-middleware的使用
2018/05/31 Javascript
详解在vue-test-utils中mock全局对象
2018/11/07 Javascript
js实现点击图片在屏幕中间弹出放大效果
2019/09/11 Javascript
ElementUI之Message功能拓展详解
2019/10/18 Javascript
JavaScript实现栈结构Stack过程详解
2020/03/07 Javascript
ES6对象操作实例详解
2020/05/23 Javascript
Python多进程通信Queue、Pipe、Value、Array实例
2014/11/21 Python
Python中的迭代器与生成器高级用法解析
2016/06/28 Python
Python操作MongoDB详解及实例
2017/05/18 Python
浅谈django的render函数的参数问题
2018/10/16 Python
Python魔法方法详解
2019/02/13 Python
Ubuntu18.04中Python2.7与Python3.6环境切换
2019/06/14 Python
pandas条件组合筛选和按范围筛选的示例代码
2019/08/26 Python
Python selenium的基本使用方法分析
2019/12/21 Python
python中图像通道分离与合并实例
2020/01/17 Python
python生成13位或16位时间戳以及反向解析时间戳的实例
2020/03/03 Python
Django Xadmin多对多字段过滤实例
2020/04/07 Python
python实现自动清理重复文件
2020/08/24 Python
英国珠宝钟表和家居礼品精品店:David Shuttle
2018/02/24 全球购物
2014个人年度工作总结范文
2014/12/24 职场文书
2015年企业团支部工作总结
2015/05/21 职场文书
 分享一个Python 遇到数据库超好用的模块
2022/04/06 Python
青岛市的收音机研制与生产
2022/04/07 无线电