当前位置: 编程技术>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; el.........
▪php IP及IP段进行访问限制的代码
192.168.1.1 单个IP 192.168.1.* 这样代理 192.168.1.1-192.168.1-255 192.158.1.2-20 这样是代表192.158.1.2-192.158.1.20 也可以这样写 192.168.1.[1|2|3] 嘿嘿~一个方法不知道想法是否周全,拿出来大家讨论 使用 以.........
▪php mssql 数据库分页SQL语句
我们在编写MIS系统和Web应用程序等系统时,都涉及到与数据库的交互,如果数据库中数据量很大的话,一次检索所有的记录,会占用系统很大的资源,因此我们常常采用,需要多少数据就只.........
[1]PHP 采集程序 常用函数
来源: 互联网 发布时间: 2013-11-30
当前的脚本网址
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(/blog_article/$code/index.html){
preg_match_all('/<a\s+href=/blog_article/[/index.html"|\']?([^>"\' ]+)["|\']?\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];
}
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(/blog_article/$code/index.html){
preg_match_all('/<a\s+href=/blog_article/[/index.html"|\']?([^>"\' ]+)["|\']?\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];
}
[2]php IP及IP段进行访问限制的代码
来源: 互联网 发布时间: 2013-11-30
192.168.1.1 单个IP
192.168.1.* 这样代理 192.168.1.1-192.168.1-255
192.158.1.2-20 这样是代表192.158.1.2-192.158.1.20
也可以这样写 192.168.1.[1|2|3]
嘿嘿~一个方法不知道想法是否周全,拿出来大家讨论
使用
以下是引用片段:
$oBlock_ip = new block_ip();
$oBlock_ip->checkIP();
以下是引用片段:
<?php
class block_ip {
var $Block_ip = array("192.168.1.1","210.10.2.1-20","222.34.4.*");
function __construct(){
}
function __destruct(){
}
private function makePregIP($str){
if (strstr($str,"-")) {
$aIP = explode(".",$str);
foreach ($aIP as $k=>$v) {
if (!strstr($v,"-")) {
$preg_limit .= makePregIP($v);
} else{
$aipNum = explode("-",$v);
for($i=$aipNum[0];$i<=$aipNum[1];$i++){
$preg .=$preg?"|".$i:"[".$i;
}
$preg_limit .=strrpos($preg_limit,".",1)==(strlen($preg_limit)-1)?$preg."]":".".$preg."]";
}
}
}else{
$preg_limit .= $str.".";
}
return $preg_limit;
}
private function getAllBlockIP(){
if ($this->Block_ip) {
foreach ($this->Block_ip as $k=>$v) {
$ipaddres = $this->makePregIP($v->start_ip);
$ip = str_ireplace(".","\.",$ipaddres);
$ip = str_replace("*","[0-9]{1,3}",$ip);
$ipaddres = "/".$ip."/";
$ip_list[] = $ipaddres;
}
}
return $ip_list;
}
public function checkIP() {
$iptable = $this->getAllBlockIP();
$IsJoined = true;
//取得用户ip
$Ip = $this->get_client_ip();
$Ip = trim($Ip);
//剔除黑名单中的IP区段
if ($iptable) {
foreach($iptable as $value) {
if (preg_match("{$value}",$Ip)) {
$IsJoined = false;
break;
}
}
}
//如果在ip黑名单中就执行如下操作
if( !$IsJoined ){
echo "IP Error";
exit;
}
}
private function get_client_ip(){
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return($ip);
}
}
?>
192.168.1.* 这样代理 192.168.1.1-192.168.1-255
192.158.1.2-20 这样是代表192.158.1.2-192.158.1.20
也可以这样写 192.168.1.[1|2|3]
嘿嘿~一个方法不知道想法是否周全,拿出来大家讨论
使用
以下是引用片段:
$oBlock_ip = new block_ip();
$oBlock_ip->checkIP();
以下是引用片段:
代码如下:
<?php
class block_ip {
var $Block_ip = array("192.168.1.1","210.10.2.1-20","222.34.4.*");
function __construct(){
}
function __destruct(){
}
private function makePregIP($str){
if (strstr($str,"-")) {
$aIP = explode(".",$str);
foreach ($aIP as $k=>$v) {
if (!strstr($v,"-")) {
$preg_limit .= makePregIP($v);
} else{
$aipNum = explode("-",$v);
for($i=$aipNum[0];$i<=$aipNum[1];$i++){
$preg .=$preg?"|".$i:"[".$i;
}
$preg_limit .=strrpos($preg_limit,".",1)==(strlen($preg_limit)-1)?$preg."]":".".$preg."]";
}
}
}else{
$preg_limit .= $str.".";
}
return $preg_limit;
}
private function getAllBlockIP(){
if ($this->Block_ip) {
foreach ($this->Block_ip as $k=>$v) {
$ipaddres = $this->makePregIP($v->start_ip);
$ip = str_ireplace(".","\.",$ipaddres);
$ip = str_replace("*","[0-9]{1,3}",$ip);
$ipaddres = "/".$ip."/";
$ip_list[] = $ipaddres;
}
}
return $ip_list;
}
public function checkIP() {
$iptable = $this->getAllBlockIP();
$IsJoined = true;
//取得用户ip
$Ip = $this->get_client_ip();
$Ip = trim($Ip);
//剔除黑名单中的IP区段
if ($iptable) {
foreach($iptable as $value) {
if (preg_match("{$value}",$Ip)) {
$IsJoined = false;
break;
}
}
}
//如果在ip黑名单中就执行如下操作
if( !$IsJoined ){
echo "IP Error";
exit;
}
}
private function get_client_ip(){
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return($ip);
}
}
?>
[3]php mssql 数据库分页SQL语句
来源: 互联网 发布时间: 2013-11-30
我们在编写MIS系统和Web应用程序等系统时,都涉及到与数据库的交互,如果数据库中数据量很大的话,一次检索所有的记录,会占用系统很大的资源,因此我们常常采用,需要多少数据就只从数据库中取多少条记录,即采用分页语句。根据自己使用过的内容,把常见数据库Sql Server,Oracle和MySQL的分页语句,从数据库表中的第M条数据开始取N条记录的语句总结如下:
SQL Server
从数据库表中的第M条记录开始取N条记录,利用Top关键字:注意如果Select语句中既有top,又有order by,则是从排序好的结果集中选择:
SELECT *
FROM ( SELECT Top N *
FROM (SELECT Top (M + N - 1) * FROM 表名称 Order by 主键 desc) t1 ) t2
Order by 主键 asc
实例:
select * from ( select TOP pagesize * FROM ( SELECT TOP pagesize*cureentpage * from user_table ORDER BY id ASC ) as aSysTable ORDER BY id DESC ) as bSysTable ORDER BY id ASC
例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
SELECT *
FROM ( SELECT TOP 20 *
FROM (SELECT TOP 29 * FROM Sys_option order by sys_id desc) t1) t2
Order by sys_id asc
Oralce数据库
从数据库表中第M条记录开始检索N条记录
SELECT *
FROM (SELECT ROWNUM r,t1.* From 表名称 t1 where rownum < M + N) t2
where t2.r >= M
例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
SELECT *
FROM (SELECT ROWNUM R,t1.* From Sys_option where rownum < 30 ) t2
Where t2.R >= 10
MySQL数据库
My sql数据库最简单,是利用mysql的LIMIT函数,LIMIT [offset,] rows从数据库表中M条记录开始检索N条记录的语句为:
SELECT * FROM 表名称 LIMIT M,N
例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
select * from sys_option limit 10,20
SQL Server
从数据库表中的第M条记录开始取N条记录,利用Top关键字:注意如果Select语句中既有top,又有order by,则是从排序好的结果集中选择:
SELECT *
FROM ( SELECT Top N *
FROM (SELECT Top (M + N - 1) * FROM 表名称 Order by 主键 desc) t1 ) t2
Order by 主键 asc
实例:
select * from ( select TOP pagesize * FROM ( SELECT TOP pagesize*cureentpage * from user_table ORDER BY id ASC ) as aSysTable ORDER BY id DESC ) as bSysTable ORDER BY id ASC
例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
SELECT *
FROM ( SELECT TOP 20 *
FROM (SELECT TOP 29 * FROM Sys_option order by sys_id desc) t1) t2
Order by sys_id asc
Oralce数据库
从数据库表中第M条记录开始检索N条记录
SELECT *
FROM (SELECT ROWNUM r,t1.* From 表名称 t1 where rownum < M + N) t2
where t2.r >= M
例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
SELECT *
FROM (SELECT ROWNUM R,t1.* From Sys_option where rownum < 30 ) t2
Where t2.R >= 10
MySQL数据库
My sql数据库最简单,是利用mysql的LIMIT函数,LIMIT [offset,] rows从数据库表中M条记录开始检索N条记录的语句为:
SELECT * FROM 表名称 LIMIT M,N
例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
select * from sys_option limit 10,20
最新技术文章: