PHP 采集程序中常用的函数_php技巧

复制代码 代码如下:

//获得当前的脚本网址
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];
}

时间: 2024-08-02 10:43:49

PHP 采集程序中常用的函数_php技巧的相关文章

PHP 中英文混合排版中处理字符串常用的函数_php技巧

# 判断某个位置是中文字符的左还是右半部分,或不是中文  # 返回值 -1 左 0 不是中文字符 1 右  # 用法  /*  $a = 'this is 中文';  print is_chinese($a, 1); // 0  print is_chinese($a,8); // -1  print is_chinese($a,9); // 1  */  function is_chinese(&$str, $location) {  $ch = true;  $i = $location; 

如何使用PHP中的字符串函数_php技巧

PHP中的字符串操作功能是比较多的,重要的有以下这些:    (1)echo,print,printf,sprintf   前两个函数是输出字符串.字符串中如果有变量名则被替换成其值.    后两个函数类似于C的同名函数.   (2)strchr,strlen,strtok,strrchr,strrev,strstr,strtolower,   strtoupper,substr,ucfirst   这些是常用的字符串操作函数,有些和C中的同名函数意义完全一致.    strrev是把一个字符串

PHP的几个常用加密函数_php技巧

MD5加密: string md5 ( string $str [, bool $raw_output = false ] ) 1.md5()默认情况下以 32 字符十六进制数字形式返回散列值,它接受两个参数,第一个为要加密的字符串,第二个为raw_output的布尔值,默认为false,如果设置为true,md5()则会返回原始的 16 位二进制格式报文摘要 2.md5()为单向加密,没有逆向解密算法,但是还是可以对一些常见的字符串通过收集,枚举,碰撞等方法破解 <?php $username

解析php中的escape函数_php技巧

采用js对URL中的汉字进行escape编码. <a href="" onclick="window.open('product_list.php?p_sort='+escape(''));">这样点击链接后的效时: 引用:http://127.0.0.1/shop/product_list.php?p_sort=PHP%u5F00%u53D1%u8D44%u6E90%u7F51生成了这样的效果, 很明显用PHP的urldecode()或者base64_

解析如何屏蔽php中的phpinfo()函数_php技巧

我们配置php环境的时候往往都会写phpinfo():这个函数来测试php环境是否安装成功,但往往这个函数也会给系统带来安全隐患,那么如何让关掉这个函数呢?下面介绍一种方法:修改php.ini文件,在里面找到如下行, 复制代码 代码如下: disable_functions = ; This directive allows you to disable certain; functions for security reasons. It receives; a comma separated

通达OA公共代码 php常用检测函数_php技巧

check_type.php(使用类型检验函数) 复制代码 代码如下: <?php /*********************/ /* */ /* Version : 5.1.0 */ /* Author : RM */ /* Comment : 071223 */ /* */ /*********************/ function is_number( $str ) { if ( substr( $str, 0, 1 ) == "-" ) { $str = subs

深入解析php中的foreach函数_php技巧

Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式.foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息. 有两种语法: 复制代码 代码如下: foreach (array_expression as $value)    statementforeach (array_expression as $key => $value)    statement 例子1: 复制代码 代码如下: <?php$a

Laravel与CI框架中截取字符串函数_php技巧

Laravel: function limit($value, $limit = 100, $end = '...') { if (mb_strwidth($value, 'UTF-8') <= $limit) { return $value; } return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end; } Ci: function word_limiter($str, $limit = 100, $end_char =

如何在PHP程序中防止盗链_php技巧

example:     页面: dl.php      --------------------------------------------------------------------------------------      复制代码 代码如下: <?php       $id = $_GET['id'];       $act = $_GET['act'];       switch($act) {       default :       case "display&