php

PHP字符串相似度

similar_text() 函数计算两个字符串的匹配字符的数目,也可以计算两个字符串的相似度(以百分比计)。

similar_text ( string first, string second [, float percent])//比较相似度 存放于$percent
if($percent>90) {…} //相似度


检查字符串是否是UTF8编码

/**
 +----------------------------------------------------------
 * 检查字符串是否是UTF8编码
 +----------------------------------------------------------
 * @param string $string 字符串
 +----------------------------------------------------------
 * @return Boolean 
 +----------------------------------------------------------
 */
function is_utf8($string)
{
    return preg_match('%^(?:
         [\x09\x0A\x0D\x20-\x7E]            # ASCII
       | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
       |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
       | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
       |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
       |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
       | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
       |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
   )*$%xs', $string);
}


array转化为ul li

function recursion($multi_dimensional_array)
{
$m = $multi_dimensional_array;
$keys = array();
foreach($m as $key=>$value)
{
$keys[] = $key;
}
$i = 0;
while($i < count($multi_dimensional_array))
{
echo ‘<li><a href=”#”>’.$keys[$i].'</a>’;
if(is_array($multi_dimensional_array[$keys[$i]]))
{
echo ‘<ul>’;
recursion($multi_dimensional_array[$keys[$i]]);
echo ‘</ul>’;
}
echo ‘</li>’;
$i++;
}
}

配合jquery-treeview效果如下:


php遍历文件夹并存入数组

<?php
$dir = dirname(__FILE__);
function read_dir_all($dir) {
$ret = array(‘dirs’=>array(), ‘files’=>array());
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if($file != ‘.’ && $file !== ‘..’) {
$cur_path = $dir . DIRECTORY_SEPARATOR . $file;
if(is_dir($cur_path)) {
$ret[‘dirs’][$cur_path] = read_dir_all($cur_path);
} else {
$ret[‘files’][] = $cur_path;
}
}
}
closedir($handle);
}
return $ret;
}

$p = read_dir_all($dir);
echo ‘<pre>’;
var_dump($p);
echo ‘</pre>’;
?>

优化版:

function read_dir_all($dir) {
$ret = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if($file != ‘.’ && $file !== ‘..’) {
$cur_path = $dir . DIRECTORY_SEPARATOR . $file;
if(is_dir($cur_path)) {
$ret[$cur_path] = read_dir_all($cur_path);
} else {
$ret[$cur_path] = $cur_path;
}
}
}
closedir($handle);
}
return $ret;
}
(continue reading…)


php文件搜索

<?php
set_time_limit(0);

function searchfromtime($dir,$fromtime,$totime) {
    $handle=opendir($dir);
    if($handle){

        while(($file=readdir($handle))!==false){

            if($file!=”.” && $file!=”..”){

                $file=$dir .’\\’. $file;

                if(is_dir($file)){

                    searchfromtime($file,$fromtime,$totime);
                }else{
     $time = filemtime($file);
     if(strtotime($fromtime)<=$time&&$time<=($totime?strtotime($totime):time())){
      echo $file.'<br/>’;
     }
                }
            }
        }
    }
}

echo searchfromtime(dirname(__FILE__),’2010-05-24′,’2010-06-07′);
?>

1 Comment more...

php计划任务php中的ignore_user_abort实现计划任务

函数-ignore_user_abort,这个函数可以帮助我们实现像linux中的cron一样实现计划任务,下面一起来看下该如何来实现。

首先看下php手册对这个函数的解释 (continue reading…)


preg_replace

preg_replace(“/<hr.*?>/”,”###SEPARATE###”,$this->pi_getFFvalue($this->cObj->data[‘pi_flexform’], ‘description’ , ‘text’));

加?号替换所有,不加?只替换第一个。


php切图

<?php
header(‘Content-type: image/jpg’);
$filename=”test.jpg”;   //要切割的大图 (continue reading…)


PHP的ASCII码转换类

/**
    *  ascii encode
	*/
	function asciiencode($c){
		$len = strlen($c);
		$a = 0;
		while ($a < $len)
		{
			$ud = 0;
			if (ord($c{$a}) >=0 && ord($c{$a})<=127)
			{
				$ud = ord($c{$a});
				$a += 1;
			}
			elseif (ord($c{$a}) >=192 && ord($c{$a})<=223)
			{
				$ud = (ord($c{$a})-192)*64 + (ord($c{$a+1})-128);
				$a += 2;
			}
			else if (ord($c{$a}) >=224 && ord($c{$a})<=239)
			{
				$ud = (ord($c{$a})-224)*4096 + (ord($c{$a+1})-128)*64 + (ord($c{$a+2})-128);
				$a += 3;
			}
			else if (ord($c{$a}) >=240 && ord($c{$a})<=247)
			{
				$ud = (ord($c{$a})-240)*262144 + (ord($c{$a+1})-128)*4096 + (ord($c{$a+2})-128)*64 + (ord($c{$a+3})-128);
				$a += 4;
			}
			else if (ord($c{$a}) >=248 && ord($c{$a})<=251)
			{
				$ud = (ord($c{$a})-248)*16777216 + (ord($c{$a+1})-128)*262144 + (ord($c{$a+2})-128)*4096 + (ord($c{$a+3})-128)*64 + (ord($c{$a+4})-128);
				$a += 5;
			}
			else if (ord($c{$a}) >=252 && ord($c{$a})<=253)
			{
				$ud = (ord($c{$a})-252)*1073741824 + (ord($c{$a+1})-128)*16777216 + (ord($c{$a+2})-128)*262144 + (ord($c{$a+3})-128)*4096 + (ord($c{$a+4})-128)*64 + (ord($c{$a+5})-128);
				$a += 6;
			}
			else if (ord($c{$a}) >=254 && ord($c{$a})<=255)
			{ //error
				$ud = false;
			}
			$scill .= "&#$ud;";
		}
		return $scill;
	}
$content = html_entity_decode(asciiencode($row['subcontent']));
< ?php
class ascii
{

function decode($str)
{
preg_match_all( “/(d{2,5})/”, $str,$a);
$a = $a[0];
foreach ($a as $dec)
{
if ($dec < 128)
{
$utf .= chr($dec);
}
else if ($dec < 2048)
{
$utf .= chr(192 + (($dec – ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
}
else
{
$utf .= chr(224 + (($dec – ($dec % 4096)) / 4096));
$utf .= chr(128 + ((($dec % 4096) – ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
}
}
return $utf;
}

Imagemagick黑白图片

convert -set option:modulate:colorspace rgb -modulate 100,0 D:/wamp/www/test.jpg D:/wamp/www/test1.jpg

typo3:

$picConf['image.']['file.']['params'] = ' -set option:modulate:colorspace rgb -modulate 100,0 ';

http://www.imagemagick.org/script/command-line-options.php#modulate

泛黄图片:
convert -set option:modulate:colorspace sRGB -modulate 100,0 -fill “rgb(237,170,73)” -tint 100 -modulate 100,80,104 -quality 100 test1.jpg test2.jpg


Copyright © 1996-2010 Add Lives. All rights reserved.
iDream theme by Templates Next | Powered by WordPress