php 文本转HTML与获取IP函数

 更新时间:2016年11月25日 15:13  点击:2139
php 文本转HTML与获取IP函数

/**
 * 文本转HTML
 *
 * @param string $txt;
 * return string;
 */
function Text2Html($txt){
 $txt = str_replace("  "," ",$txt);
 $txt = str_replace("<","&lt;",$txt);
 $txt = str_replace(">","&gt;",$txt);
 $txt = preg_replace("/[rn]{1,}/isU","<br/>rn",$txt);
 return $txt;
}

/**
 * 获得IP
 * return string;
 */
function GetIP(){
 if(!empty($_SERVER["HTTP_CLIENT_IP"])) { $cip = $_SERVER["HTTP_CLIENT_IP"]; }
 else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) { $cip = $_SERVER["HTTP_X_FORWARDED_FOR"]; }
 else if(!empty($_SERVER["REMOTE_ADDR"])) { $cip = $_SERVER["REMOTE_ADDR"]; }
 else $cip = "";
 preg_match("/[d.]{7,15}/", $cip, $cips);
 $cip = $cips[0] ? $cips[0] : 'unknown';
 unset($cips);
 return $cip;
}

计算字符串的长度(汉字按照两个字符计算)


 *
 * @param   string      $str        字符串
 *
 * @return  int
 */
function str_len($str)
{
    $length = strlen(preg_replace('/[x00-x7F]/', '', $str));

    if ($length)
    {
        return strlen($str) - $length + intval($length / 3) * 2;
    }
    else
    {
        return strlen($str);
    }
}

php ubb代码转换函数


<?php
function ubb($string) {
  $searcharray['bbcode_regexp'] = array(
   "/s*[quote][nr]*(.+?)[nr]*[/quote]s*/is",
   "/([box=(#[0-9A-F]{6}|[a-z]+)])[nr]*(.+?)[nr]*([/box])/is",
   "/[url]s*(www.|https?://|ftp://|gopher://|news://|telnet://|rtsp://|mms://){1}([^["']+?)s*[/url]/ie",
   "/[url=www.([^["']+?)](.+?)[/url]/is",
   "/[url=(https?|ftp|gopher|news|telnet|rtsp|mms){1}://([^["']+?)](.+?)[/url]/is",
   "/[email]s*([A-Za-z0-9-_.]+)@([A-Za-z0-9-_]+[.][A-Za-z0-9-_.]+)s*[/email]/i",
   "/[email=([A-Za-z0-9-_.]+)@([A-Za-z0-9-_]+[.][A-Za-z0-9-_.]+)](.+?)[/email]/is",
   "/[color=([^[]+?)]/i",
   "/[size=([^[]+?)]/i",
   "/[font=([^[]+?)]/i",
   "/[align=([^[]+?)]/i",
   "/[center]/i",
   "/[swf]s*([^[]+?)s*[/swf]/ies",
   "/[img]s*([^[]+?)s*[/img]/ies",
   "/[img=(d{1,3})[x|,](d{1,3})]s*([^[]+?)s*[/img]/ies"
  );
  $replacearray['bbcode_regexp'] = array(
   "<br><br><center><table border="0" width="90%" cellspacing="0" cellpadding="0"><tr><td>&nbsp;&nbsp;Quote:</td></tr><tr><td><table border="0" width="100%" cellspacing="1" cellpadding="10" bgcolor="".BORDERCOLOR.""><tr><td width="100%" bgcolor="".ALTBG2."" style="word-break:break-all">\1</td></tr></table></td></tr></table></center><br>",
   "<blockquote style="background-color: \2 ;"><span class="bold">$title</span>\3</blockquote>",
   "cuturl('\1\2')",
   "<a href="http://www.\1" target="_blank">\2</a>",
   "<a href="\1://\2" target="_blank">\3</a>",
   "<a href="mailto:\1@\2">\1@\2</a>",
   "<a href="mailto:\1@\2">\3</a>",
   "<font color="\1">",
   "<font size="\1">",
   "<font face="\1">",
   "<p align="\1">",
   "<p align="center">",
   "bbcodeurl('\1', ' <img src="images/flash.gif" align="absmiddle"> <a href="%s" target="_blank">Flash: %s</a> ')",
   "bbcodeurl('\1', '<img src="%s" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window';}" onmouseover="if(this.resized) this.style.cursor='hand';" onclick="if(this.resized) {window.open('%s');}">')",
   "bbcodeurl('\3', '<img width="\1" height="\2" src="%s" border="0">')"
  );

  $searcharray['bbcode_str'] = array(
   '[/color]', '[/size]', '[/font]', '[/align]', '[b]', '[/b]',
   '[i]', '[/i]', '[u]', '[/u]', '[list]', '[list=1]', '[list=a]',
   '[list=A]', '[*]', '[/list]','[/center]'
  );

  $replacearray['bbcode_str'] = array(
   '</font>', '</font>', '</font>', '</p>', '<b>', '</b>', '<i>',
   '</i>', '<u>', '</u>', '<ul>', '<ol type=1>', '<ol type=a>',
   '<ol type=A>', '<li>', '</ul></ol>','</p>'
  );                 
  $string = str_replace($searcharray['bbcode_str'], $replacearray['bbcode_str'], preg_replace($searcharray['bbcode_regexp'], $replacearray['bbcode_regexp'], $string));

                return $string;
}

function bbcodeurl($url, $tags) {
 if(!preg_match("/<.+?>/s",$url)) {
  if(!in_array(strtolower(substr($url, 0, 6)), array('http:/', 'ftp://', 'rtsp:/', 'mms://'))) {
   $url = 'http://'.$url;
  }
  return str_replace('submit', '', sprintf($tags, $url, $url));
 } else {
  return '&nbsp;'.$url;
 }
}

function cuturl($url) {
 $length = 65;
 $urllink = "<a href="".(substr(strtolower($url), 0, 4) == 'www.' ? "http://$url" : $url).'" target="_blank">';
 if(strlen($url) > $length) {
  $url = substr($url, 0, intval($length * 0.5)).' ... '.substr($url, - intval($length * 0.3));
 }
 $urllink .= $url.'</a>';
 return $urllink;
}
?>

php 生成验证码源码


session_start();

$enablegd = 1;
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
foreach($funcs as $func)
{
 if(!function_exists($func))
 {
  $enablegd = 0;
  break;
 }
}

ob_clean();

if($enablegd)
{
 //create captcha
 $consts = 'cdfgkmnpqrstwxyz23456';
 $vowels = 'aek23456789';
 for ($x = 0; $x < 6; $x++)
 {
  $const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1);
  $vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1);
 }
 $radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
 $_SESSION['checkcode'] = $string = substr($radomstring,0,4); //only display 4 str
 //set up image, the first number is the width and the second is the height
 $imageX = strlen($radomstring)*8; //the image width
 $imageY = 20;      //the image height
 $im = imagecreatetruecolor($imageX,$imageY);

 //creates two variables to store color
 $background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250));
 $foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
         imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
         imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
         imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255)));
 $foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80);
 $middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160));
 $middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80);

 //fill image with bgcolor
 imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
 //writes string
 imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], PHPCMS_ROOT.'include/fonts/ALGER.TTF', $string[0]);
 imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], PHPCMS_ROOT.'include/fonts/ARIALNI.TTF', $string[1]);
 imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], PHPCMS_ROOT.'include/fonts/ALGER.TTF', $string[2]);
 imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], PHPCMS_ROOT.'include/fonts/arial.ttf', $string[3]);
 
 //strikethrough

 $border = imagecolorallocate($im, 133, 153, 193);
 //imagefilledrectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $back);
 imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);

 $pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
 for ($i=0;$i<80;$i++)
 {
  imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
 }
 //random shapes
 for ($x=0; $x<9;$x++)
 {
  if(mt_rand(0,$x)%2==0)
  {
   imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
   imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
  }
  else
  {
   imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
   imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
  }
 }
 //output to browser
    header("content-type:image/pngrn");
 imagepng($im);
 imagedestroy($im);
}
else
{
 $files = glob(PHPCMS_ROOT.'images/checkcode/*.jpg');
 if(!is_array($files)) exit($LANG['please_check_dir_images_checkcode']);

 $checkcodefile = $files[rand(0, count($files)-1)];
 $_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4);

 header("content-type:image/jpegrn");
 include $checkcodefile;
}

php简单文件下载过滤判断函数

if(strpos($fileurl, ':/'))//远程文件
{
 header("Location: $fileurl");
}
else//本地文件
{
 if($d == 0)
 {
  header("Location: ".SITE_URL.$fileurl);
 }
 else
 {
  $fileurl = file_exists($fileurl) ? stripslashes($fileurl) : PHPCMS_ROOT.$fileurl;//此处可能为物理路径
  $filename = basename($fileurl);
  if(preg_match("/^([sS]*?)([x81-xfe][x40-xfe])([sS]*?)/", $fileurl))//处理中文文件
  {
   $filename = str_replace(array("%5C", "%2F", "%3A"), array("\", "/", ":"), urlencode($fileurl));
   $filename = urldecode(basename($filename));
  }
  file_down($fileurl, $filename);
 }
}

[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • PHP成员变量获取对比(类成员变量)

    下面本文章来给大家介绍在php中成员变量的一些对比了,文章举了四个例子在这例子中分别对不同成员变量进行测试与获取操作,下面一起来看看。 有如下4个代码示例,你认...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • php 获取用户IP与IE信息程序

    php 获取用户IP与IE信息程序 function onlineip() { global $_SERVER; if(getenv('HTTP_CLIENT_IP')) { $onlineip = getenv('HTTP_CLIENT_IP');...2016-11-25
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • php获取一个文件夹的mtime的程序

    php获取一个文件夹的mtime的程序了,这个就是时间问题了,对于这个问题我们来看小编整理的几个例子,具体的操作例子如下所示。 php很容易获取到一个文件夹的mtime,可以...2016-11-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 如何获取网站icon有哪些可行的方法

    获取网站icon,常用最简单的方法就是通过website/favicon.ico来获取,不过由于很多网站都是在页面里面设置favicon,所以此方法很多情况都不可用。 更好的办法是通过google提供的服务来实现:http://www.google.com/s2/favi...2014-06-07
  • python Matplotlib基础--如何添加文本和标注

    这篇文章主要介绍了python Matplotlib基础--如何添加文本和标注,帮助大家更好的利用Matplotlib绘制图表,感兴趣的朋友可以了解下...2021-01-26
  • jquery如何获取元素的滚动条高度等实现代码

    主要功能:获取浏览器显示区域(可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 :$(window).width(); 获取页面的文档高度 $(document).height(); 获取页面的文档宽度 :$(document).width();...2015-10-21
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • jquery获取div距离窗口和父级dv的距离示例

    jquery中jquery.offset().top / left用于获取div距离窗口的距离,jquery.position().top / left 用于获取距离父级div的距离(必须是绝对定位的div)。 (1)先介绍jquery.offset().top / left css: 复制代码 代码如下: *{ mar...2013-10-13
  • Jquery 获取指定标签的对象及属性的设置与移除

    1、先讲讲JQuery的概念,JQuery首先是由一个 America 的叫什么 John Resig的人创建的,后来又很多的JS高手也加入了这个团队。其实 JQuery是一个JavaScript的类库,这个类库集合了很多功能方法,利用类库你可以用简单的一些代...2014-05-31
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28