php

去掉 HTML 标记,javascript 代码

function trip_html( $html, $len ) {
// $html 应包含一个 HTML 文档。
// 本例将去掉 HTML 标记,javascript 代码
// 和空白字符。还会将一些通用的
// HTML 实体转换成相应的文本。
$search = array (“‘<script[^>]*?>.*?</script>’si”,  // 去掉 javascript
“‘<[\/\!]*?[^<>]*?>’si”,           // 去掉 HTML 标记
“‘([\r\n])[\s]+’”,                 // 去掉空白字符
“‘&(quot|#34);’i”,                 // 替换 HTML 实体
“‘&(amp|#38);’i”,
“‘&(lt|#60);’i”,
“‘&(gt|#62);’i”,
“‘&(nbsp|#160);’i”,
“‘&(iexcl|#161);’i”,
“‘&(cent|#162);’i”,
“‘&(pound|#163);’i”,
“‘&(copy|#169);’i”,
“‘&#(\d+);’e”);                    // 作为 PHP 代码运行
$replace = array (“”,
“”,
“\\1″,
“\”",
“&”,
“<”,
“>”,
“ ”,
chr(161),
chr(162),
chr(163),
chr(169),
“chr(\\1)”);
$text = preg_replace ($search, $replace, $html);
$text = trim($text);
return mb_strlen($text) >= $len ? mb_substr($text, 0, $len) : ”;
}

Popularity: 21%

asp

ASP来实现UTF8转换GB2312国标码

ASP来实现UTF8转换GB2312国标码-GB2312转UTF-8

‘个人代码风格注释(变量名中第一个小写字母表表示变量类型)
‘i:为Integer型;
’s:为String;
Function U2UTF8(Byval a_iNum)
    Dim sResult,sUTF8
    Dim iTemp,iHexNum,i

    iHexNum = Trim(a_iNum)

    If iHexNum = “” Then
        Exit Function
    End If

    sResult = “”

    If (iHexNum < 128) Then
        sResult = sResult & iHexNum
    ElseIf (iHexNum < 2048) Then
        sResult = ChrB(&H80 + (iHexNum And &H3F))
        iHexNum = iHexNum \ &H40
        sResult = ChrB(&HC0 + (iHexNum And &H1F)) & sResult
    ElseIf (iHexNum < 65536) Then
        sResult = ChrB(&H80 + (iHexNum And &H3F))
        iHexNum = iHexNum \ &H40
        sResult = ChrB(&H80 + (iHexNum And &H3F)) & sResult
        iHexNum = iHexNum \ &H40
        sResult = ChrB(&HE0 + (iHexNum And &HF)) & sResult
    End If

    U2UTF8 = sResult
End Function

Function GB2UTF(Byval a_sStr)
    Dim sGB,sResult,sTemp
    Dim iLen,iUnicode,iTemp,i

    sGB = Trim(a_sStr)
    iLen = Len(sGB)
    For i = 1 To iLen
         sTemp = Mid(sGB,i,1)
         iTemp = Asc(sTemp)

         If (iTemp>127 OR iTemp<0) Then
             iUnicode = AscW(sTemp)
             If iUnicode<0 Then
                 iUnicode = iUnicode + 65536
             End If
        Else
            iUnicode = iTemp
        End If

        sResult = sResult & U2UTF8(iUnicode)
    Next

    GB2UTF = sResult
End Function

‘调用方法
Response.BinaryWrite(GB2UTF(“中国人”))
该文章出自《TOMMYHU’s blog》,原文链接:http://www.tommyhu.cn/asp-to-achieve-utf8-conversion-gb2312-gb-code/

转载时间是: 2010-04-06 07:09:31 请务必保留此链接,谢谢!

Popularity: 19%

php

二维数组的array_unique函数

关于php的array_unique函数,此函数只适用于一维数组,对多维数组并不适用,以下提供一个二维数组的array_unique函数 

//二维数组去掉重复值
function array_unique_fb($array2D){  
     foreach ($array2D as $v){
         $v = join(“,”,$v);              //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
         $temp[] = $v;
     }

     $temp = array_unique($temp);      //去掉重复的字符串,也就是重复的一维数组
    foreach ($temp as $k => $v){
        $temp[$k] = explode(“,”,$v);       //再将拆开的数组重新组装
    }
    return $temp;
}

原文:http://blog.sina.com.cn/s/blog_60c5cbf50100e4qz.html

Popularity: 8%

php

GBK的页面输出JSON

encode之前转换为utf-8,decode之后转回gbk:

function tb_json_encode($value, $options = 0)
{
  return json_encode(tb_json_convert_encoding($value, “GBK”, “UTF-8″));
}

function tb_json_decode($str, $assoc = false, $depth = 512)
{
  return tb_json_convert_encoding(json_decode($str, $assoc), “UTF-8″, “GBK”);
}

function tb_json_convert_encoding($m, $from, $to)
{
  switch(gettype($m)) {
    case ‘integer’:
    case ‘boolean’:
    case ‘float’:
    case ‘double’:
    case ‘NULL’:
      return $m;

    case ’string’:
      return mb_convert_encoding($m, $to, $from);
    case ‘object’:
      $vars = array_keys(get_object_vars($m));
      foreach($vars as $key) {
        $m->$key = tb_json_convert_encoding($m->$key, $from ,$to);
      }
      return $m;
    case ‘array’:
      foreach($m as $k => $v) {
        $m[tb_json_convert_encoding($k, $from, $to)] = tb_json_convert_encoding($v, $from, $to);
      }
      return $m;
    default:
  }
  return $m;
}

原文:http://sanmuding.com/i/660

Popularity: 22%

php

文本转HTML

//文本转HTML
function Text2Html($txt)
{
$txt = str_replace(”  ”,” ”,$txt);
$txt = str_replace(“<”,”&lt;”,$txt);
$txt = str_replace(“>”,”&gt;”,$txt);
$txt = preg_replace(“/[\r\n]{1,}/isU”,”<br/>\r\n”,$txt);
return $txt;
}

Popularity: 9%

php

在字符串指定位置插入一段字符串

//插入一段字符串
function str_insert($str, $i, $substr)
{
for($j=0; $j<$i; $j++){
$startstr .= $str[$j];
}
for ($j=$i; $j<strlen($str); $j++){
$laststr .= $str[$j];
}
$str = ($startstr . $substr . $laststr);
return $str;
}

Popularity: 12%

php

php数组二分法查找

<?php
//search函数 其中$array为数组,$k为要找的值,$low为查找范围的最小键值,$high为查找范围的最大键值
function search($array, $k, $low=0, $high=0)
{
if(count($array)!=0 and $high == 0)                 //判断是否为第一次调用
{
$high = count($array);
}
if($low <= $high)                                //如果还存在剩余的数组元素
{
$mid = intval(($low+$high)/2);                  //取$low和$high的中间值
if ($array[$mid] == $k)                         //如果找到则返回
{
return $mid;
}
elseif ($k < $array[$mid])                       //如果没有找到,则继续查找
{
return search($array, $k, $low, $mid-1);
}
else
{
return search($array, $k, $mid+1, $high);
}
}
return -1;
}
$array = array(4,5,7,8,9,10);                           //测试search函数
echo search($array, 8);                             //调用search函数并输出查找结果
?>

原文:http://blog.csdn.net/zhuzhao/archive/2009/03/09/3969044.aspx

Popularity: 10%

php

正确获得访客IP

if(getenv(‘HTTP_CLIENT_IP’) && strcasecmp(getenv(‘HTTP_CLIENT_IP’), ‘unknown’)) {
$onlineip = getenv(‘HTTP_CLIENT_IP’);
} elseif(getenv(‘HTTP_X_FORWARDED_FOR’) && strcasecmp(getenv(‘HTTP_X_FORWARDED_FOR’), ‘unknown’)) {
$onlineip = getenv(‘HTTP_X_FORWARDED_FOR’);
} elseif(getenv(‘REMOTE_ADDR’) && strcasecmp(getenv(‘REMOTE_ADDR’), ‘unknown’)) {
$onlineip = getenv(‘REMOTE_ADDR’);
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], ‘unknown’)) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
preg_match(“/[\d\.]{7,15}/”, $onlineip, $onlineipmatches);
$onlineip = $onlineipmatches[0] ? $onlineipmatches[0] : ‘unknown’;

Popularity: 19%

php

加强版htmlspecialchars

//取消HTML代码

function shtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = shtmlspecialchars($val);
}
} else {
$string = preg_replace(‘/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/’, ‘&\\1′,
str_replace(array(‘&’, ‘”‘, ‘<’, ‘>’), array(‘&amp;’, ‘&quot;’, ‘&lt;’, ‘&gt;’), $string));
}
return $string;
}

Popularity: 10%

php

支持数组的ADDSLASHES

//SQL ADDSLASHES
function saddslashes($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = saddslashes($val);
}
} else {
$string = addslashes($string);
}
return $string;
}

Popularity: 87%