[PHP]经常用到的实用函数集合第1/2页
程序员文章站
2022-08-29 18:06:46
最新打算学习php,发现好多函数需要用到,可自己写又麻烦,于是找了下php的常用函数,从各大php系统中弄下来的,绝对是使用的php实用函数复制代码 代码如下:functi...
最新打算学习php,发现好多函数需要用到,可自己写又麻烦,于是找了下php的常用函数,从各大php系统中弄下来的,绝对是使用的php实用函数
function getip() { //获取ip
if ($_server["http_x_forwarded_for"])
$ip = $_server["http_x_forwarded_for"];
else if ($_server["http_client_ip"])
$ip = $_server["http_client_ip"];
else if ($_server["remote_addr"])
$ip = $_server["remote_addr"];
else if (getenv("http_x_forwarded_for"))
$ip = getenv("http_x_forwarded_for");
else if (getenv("http_client_ip"))
$ip = getenv("http_client_ip");
else if (getenv("remote_addr"))
$ip = getenv("remote_addr");
else
$ip = "unknown";
return $ip;
}
function dateadd($date, $int, $unit = "d") { //时间的增加(还可以改进成时分秒都可以增加,有时间再补上)
$datearr = explode("-", $date);
$value[$unit] = $int;
return date("y-m-d", mktime(0,0,0, $datearr[1] + $value['m'], $datearr[2] + $value['d'], $datearr[0] + $value['y']));
}
function getweekday($date) { //计算出给出的日期是星期几
$datearr = explode("-", $date);
return date("w", mktime(0,0,0,$datearr[1],$datearr[2],$datearr[0]));
}
function check_date($date) { //检查日期是否合法日期
$datearr = explode("-", $date);
if (is_numeric($datearr[0]) && is_numeric($datearr[1]) && is_numeric($datearr[2])) {
return checkdate($datearr[1],$datearr[2],$datearr[0]);
}
return false;
}
function check_time($time) { //检查时间是否合法时间
$timearr = explode(":", $time);
if (is_numeric($timearr[0]) && is_numeric($timearr[1]) && is_numeric($timearr[2])) {
if (($timearr[0] >= 0 && $timearr[0] <= 23) && ($timearr[1] >= 0 && $timearr[1] <= 59) && ($timearr[2] >= 0 && $timearr[2] <= 59))
return true;
else
return false;
}
return false;
}
function datediff($date1, $date2, $unit = "") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天
switch ($unit) {
case 's':
$dividend = 1;
break;
case 'i':
$dividend = 60;
break;
case 'h':
$dividend = 3600;
break;
case 'd':
$dividend = 86400;
break;
default:
$dividend = 86400;
}
$time1 = strtotime($date1);
$time2 = strtotime($date2);
if ($time1 && $time2)
return (float)($time1 - $time2) / $dividend;
return false;
}
function get_innerhtml($html,$label) { //获取一对html标记间的html字符串
$result_arr = preg_split("/<\/".$label.">/i",$html);
$pattern = "/<".$label.".*?>/i";
for ($i = 0; $i < count($result_arr); $i++) {
list($left, $right) = preg_split($pattern,$result_arr[$i],2);
$result_arr[$i] = $right;
}
return $result_arr;
}
//例: echo get_innerhtml("<tr><td height=20>something</td></tr>", "td"); //will print "something".
function get_input_value($input) { //获取input的html代码中的value值
$pos = stripos($input, "value=") + 6;
if ($pos !== false) {
$input = substr($input, $pos);
if (substr($input, 0, 1) == "\"")
return substr($input, 1, strpos($input, "\"", 1) - 1);
else
return substr($input, 0, strpos($input, " ") - 1);
}
return false;
}
function getcontentbetween($a, $b, $str) { //获取字符串$str中,字符串$a与字符串$b之间的字符串
if ($str!=="" && $a!=="" && $b!=="") {
$start = strpos($str, $a) + strlen($a);
return substr($str, $start, strpos($str, $b, $start + 1) - $start);
}
return false;
}
1、关于php重定向
方法一:header("location: index.php");
方法二:echo "<scrīpt>window.location ="$php_self";</scrīpt>";
方法三:echo "<meta http-equiv="refresh" content="0; url=index.php">";
2、获取访问者浏览器
function browse_infor()
{
$browser="";$browserver="";
$browsers =array("lynx","mosaic","aol","opera","java","macweb","webexplorer","omniweb");
$agent = $globals["http_user_agent"];
for ($i=0; $i<=7; $i++)
{
if (strpos($agent,$browsers[$i]))
{
$browser = $browsers[$i];
$browserver ="";
}
}
if (ereg("mozilla",$agent) && !ereg("msie",$agent))
{
$temp =explode("(", $agent); $part=$temp[0];
$temp =explode("/", $part); $browserver=$temp[1];
$temp =explode(" ",$browserver); $browserver=$temp[0];
$browserver =preg_replace("/([d.]+)/","1",$browserver);
$browserver = " $browserver";
$browser = "netscape navigator";
}
if (ereg("mozilla",$agent) && ereg("opera",$agent))
{
$temp =explode("(", $agent); $part=$temp[1];
$temp =explode(")", $part); $browserver=$temp[1];
$temp =explode(" ",$browserver);$browserver=$temp[2];
$browserver =preg_replace("/([d.]+)/","1",$browserver);
$browserver = " $browserver";
$browser = "opera";
}
if (ereg("mozilla",$agent) && ereg("msie",$agent))
{
$temp = explode("(", $agent); $part=$temp[1];
$temp = explode(";",$part); $part=$temp[1];
$temp = explode(" ",$part);$browserver=$temp[2];
$browserver =preg_replace("/([d.]+)/","1",$browserver);
$browserver = " $browserver";
$browser = "internet explorer";
}
if ($browser!="")
{
$browseinfo = "$browser$browserver";
}
else
{
$browseinfo = "unknown";
}
return $browseinfo;
}
//调用方法$browser=browseinfo() ;直接返回结果
3、获取访问者操作系统
function osinfo() {
$os="";
$agent = $globals["http_user_agent"];
if (eregi('win',$agent) && strpos($agent, '95')) {
$os="windows 95";
}
elseif (eregi('win 9x',$agent) && strpos($agent, '4.90')) {
$os="windows me";
}
elseif (eregi('win',$agent) && ereg('98',$agent)) {
$os="windows 98";
}
elseif (eregi('win',$agent) && eregi('nt 5.0',$agent)) {
$os="windows 2000";
}
elseif (eregi('win',$agent) && eregi('nt',$agent)) {
$os="windows nt";
}
elseif (eregi('win',$agent) && eregi('nt 5.1',$agent)) {
$os="windows xp";
}
elseif (eregi('win',$agent) && ereg('32',$agent)) {
$os="windows 32";
}
elseif (eregi('linux',$agent)) {
$os="linux";
}
elseif (eregi('unix',$agent)) {
$os="unix";
}
elseif (eregi('sun',$agent) && eregi('os',$agent)) {
$os="sunos";
}
elseif (eregi('ibm',$agent) && eregi('os',$agent)) {
$os="ibm os/2";
}
elseif (eregi('mac',$agent) && eregi('pc',$agent)) {
$os="macintosh";
}
elseif (eregi('powerpc',$agent)) {
$os="powerpc";
}
elseif (eregi('aix',$agent)) {
$os="aix";
}
elseif (eregi('hpux',$agent)) {
$os="hpux";
}
elseif (eregi('netbsd',$agent)) {
$os="netbsd";
}
elseif (eregi('bsd',$agent)) {
$os="bsd";
}
elseif (ereg('osf1',$agent)) {
$os="osf1";
}
elseif (ereg('irix',$agent)) {
$os="irix";
}
elseif (eregi('freebsd',$agent)) {
$os="freebsd";
}
if ($os=='') $os = "unknown";
return $os;
}
//调用方法$os=os_infor() ;
复制代码 代码如下:
function getip() { //获取ip
if ($_server["http_x_forwarded_for"])
$ip = $_server["http_x_forwarded_for"];
else if ($_server["http_client_ip"])
$ip = $_server["http_client_ip"];
else if ($_server["remote_addr"])
$ip = $_server["remote_addr"];
else if (getenv("http_x_forwarded_for"))
$ip = getenv("http_x_forwarded_for");
else if (getenv("http_client_ip"))
$ip = getenv("http_client_ip");
else if (getenv("remote_addr"))
$ip = getenv("remote_addr");
else
$ip = "unknown";
return $ip;
}
function dateadd($date, $int, $unit = "d") { //时间的增加(还可以改进成时分秒都可以增加,有时间再补上)
$datearr = explode("-", $date);
$value[$unit] = $int;
return date("y-m-d", mktime(0,0,0, $datearr[1] + $value['m'], $datearr[2] + $value['d'], $datearr[0] + $value['y']));
}
function getweekday($date) { //计算出给出的日期是星期几
$datearr = explode("-", $date);
return date("w", mktime(0,0,0,$datearr[1],$datearr[2],$datearr[0]));
}
复制代码 代码如下:
function check_date($date) { //检查日期是否合法日期
$datearr = explode("-", $date);
if (is_numeric($datearr[0]) && is_numeric($datearr[1]) && is_numeric($datearr[2])) {
return checkdate($datearr[1],$datearr[2],$datearr[0]);
}
return false;
}
function check_time($time) { //检查时间是否合法时间
$timearr = explode(":", $time);
if (is_numeric($timearr[0]) && is_numeric($timearr[1]) && is_numeric($timearr[2])) {
if (($timearr[0] >= 0 && $timearr[0] <= 23) && ($timearr[1] >= 0 && $timearr[1] <= 59) && ($timearr[2] >= 0 && $timearr[2] <= 59))
return true;
else
return false;
}
return false;
}
function datediff($date1, $date2, $unit = "") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天
switch ($unit) {
case 's':
$dividend = 1;
break;
case 'i':
$dividend = 60;
break;
case 'h':
$dividend = 3600;
break;
case 'd':
$dividend = 86400;
break;
default:
$dividend = 86400;
}
$time1 = strtotime($date1);
$time2 = strtotime($date2);
if ($time1 && $time2)
return (float)($time1 - $time2) / $dividend;
return false;
}
复制代码 代码如下:
function get_innerhtml($html,$label) { //获取一对html标记间的html字符串
$result_arr = preg_split("/<\/".$label.">/i",$html);
$pattern = "/<".$label.".*?>/i";
for ($i = 0; $i < count($result_arr); $i++) {
list($left, $right) = preg_split($pattern,$result_arr[$i],2);
$result_arr[$i] = $right;
}
return $result_arr;
}
//例: echo get_innerhtml("<tr><td height=20>something</td></tr>", "td"); //will print "something".
function get_input_value($input) { //获取input的html代码中的value值
$pos = stripos($input, "value=") + 6;
if ($pos !== false) {
$input = substr($input, $pos);
if (substr($input, 0, 1) == "\"")
return substr($input, 1, strpos($input, "\"", 1) - 1);
else
return substr($input, 0, strpos($input, " ") - 1);
}
return false;
}
function getcontentbetween($a, $b, $str) { //获取字符串$str中,字符串$a与字符串$b之间的字符串
if ($str!=="" && $a!=="" && $b!=="") {
$start = strpos($str, $a) + strlen($a);
return substr($str, $start, strpos($str, $b, $start + 1) - $start);
}
return false;
}
1、关于php重定向
复制代码 代码如下:
方法一:header("location: index.php");
方法二:echo "<scrīpt>window.location ="$php_self";</scrīpt>";
方法三:echo "<meta http-equiv="refresh" content="0; url=index.php">";
2、获取访问者浏览器
复制代码 代码如下:
function browse_infor()
{
$browser="";$browserver="";
$browsers =array("lynx","mosaic","aol","opera","java","macweb","webexplorer","omniweb");
$agent = $globals["http_user_agent"];
for ($i=0; $i<=7; $i++)
{
if (strpos($agent,$browsers[$i]))
{
$browser = $browsers[$i];
$browserver ="";
}
}
if (ereg("mozilla",$agent) && !ereg("msie",$agent))
{
$temp =explode("(", $agent); $part=$temp[0];
$temp =explode("/", $part); $browserver=$temp[1];
$temp =explode(" ",$browserver); $browserver=$temp[0];
$browserver =preg_replace("/([d.]+)/","1",$browserver);
$browserver = " $browserver";
$browser = "netscape navigator";
}
if (ereg("mozilla",$agent) && ereg("opera",$agent))
{
$temp =explode("(", $agent); $part=$temp[1];
$temp =explode(")", $part); $browserver=$temp[1];
$temp =explode(" ",$browserver);$browserver=$temp[2];
$browserver =preg_replace("/([d.]+)/","1",$browserver);
$browserver = " $browserver";
$browser = "opera";
}
if (ereg("mozilla",$agent) && ereg("msie",$agent))
{
$temp = explode("(", $agent); $part=$temp[1];
$temp = explode(";",$part); $part=$temp[1];
$temp = explode(" ",$part);$browserver=$temp[2];
$browserver =preg_replace("/([d.]+)/","1",$browserver);
$browserver = " $browserver";
$browser = "internet explorer";
}
if ($browser!="")
{
$browseinfo = "$browser$browserver";
}
else
{
$browseinfo = "unknown";
}
return $browseinfo;
}
//调用方法$browser=browseinfo() ;直接返回结果
3、获取访问者操作系统
复制代码 代码如下:
function osinfo() {
$os="";
$agent = $globals["http_user_agent"];
if (eregi('win',$agent) && strpos($agent, '95')) {
$os="windows 95";
}
elseif (eregi('win 9x',$agent) && strpos($agent, '4.90')) {
$os="windows me";
}
elseif (eregi('win',$agent) && ereg('98',$agent)) {
$os="windows 98";
}
elseif (eregi('win',$agent) && eregi('nt 5.0',$agent)) {
$os="windows 2000";
}
elseif (eregi('win',$agent) && eregi('nt',$agent)) {
$os="windows nt";
}
elseif (eregi('win',$agent) && eregi('nt 5.1',$agent)) {
$os="windows xp";
}
elseif (eregi('win',$agent) && ereg('32',$agent)) {
$os="windows 32";
}
elseif (eregi('linux',$agent)) {
$os="linux";
}
elseif (eregi('unix',$agent)) {
$os="unix";
}
elseif (eregi('sun',$agent) && eregi('os',$agent)) {
$os="sunos";
}
elseif (eregi('ibm',$agent) && eregi('os',$agent)) {
$os="ibm os/2";
}
elseif (eregi('mac',$agent) && eregi('pc',$agent)) {
$os="macintosh";
}
elseif (eregi('powerpc',$agent)) {
$os="powerpc";
}
elseif (eregi('aix',$agent)) {
$os="aix";
}
elseif (eregi('hpux',$agent)) {
$os="hpux";
}
elseif (eregi('netbsd',$agent)) {
$os="netbsd";
}
elseif (eregi('bsd',$agent)) {
$os="bsd";
}
elseif (ereg('osf1',$agent)) {
$os="osf1";
}
elseif (ereg('irix',$agent)) {
$os="irix";
}
elseif (eregi('freebsd',$agent)) {
$os="freebsd";
}
if ($os=='') $os = "unknown";
return $os;
}
//调用方法$os=os_infor() ;
1
下一篇: 猪油要熬到什么程度?猪油容易变坏吗?