PHP輸出一個(gè)指定范圍內的隨機數
<?php echo mt_rand(5, 15); ?>
php查找字符串中出現的次數函數substr_count
判斷字符串中是否包含另一個(gè)字符串函數strpos
PHP 截取字符串函數substr()
PHP 字符串替換函數 str_replace()
PHP中.= 連接字符串
php5.4以上版本GBK編碼下htmlspecialchars輸出為空,原因在于php5.4.0對這個(gè)函數htmlspecialchars的變化,原來(lái)是ISO-8859-1,5.4后默認變成utf-8!然后中文使用這個(gè)函數就輸出為空白了。
只能使用 htmlspecialchars($str,ENT_COMPAT,'GB2312');
或者
htmlspecialchars($str,ENT_COMPAT,'ISO-8859-1'); //gbk
/**
* 中英文截取字符串,漢字安2個(gè)字節
*
* @access public
* @param string $str 需要截取的字符串
* @param int $cutLen 截取的長(cháng)度
* @param bool $cutSlashes 是否去掉\
* @param bool $addSlashes 是加\
* @param string $oDot 截取后加的字符串,如經(jīng)常用的三個(gè)點(diǎn)
* @param bool $hasHtml 是否有html
* @return string
*/
function cn_substr($str, $cutLen, $oDot = null, $hasHtml = false, $cutSlashes = false, $addSlashes = false) {
global $cfg_soft_lang;
$str = trim ( $str );
if ($cutSlashes) $str = stripslashes ( $str );
if($hasHtml) {
$str = preg_replace ("/(\<[^\<]*\>|\r|\n|\s|\[.+?\])/is",'', $str );
//$str = htmlspecialchars ( $str,ENT_COMPAT,'GB2312');
$str = htmlspecialchars ( $str,ENT_COMPAT,'ISO-8859-1');
} else {
//$str = htmlspecialchars ( $str,ENT_COMPAT,'GB2312');
$str = htmlspecialchars ( $str,ENT_COMPAT,'ISO-8859-1');
}
if ($cutLen && strlen ( $str ) > $cutLen) {
$nStr = '';
if ($cfg_soft_lang == 'utf-8') {
$n = 0;
$tn = 0;
$noc = 0;
while ( $n < strlen ( $str ) ) {
$t = ord ( $str [$n] );
if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1;
$n ++;
$noc ++;
} elseif (194 <= $t && $t <= 223) {
$tn = 2;
$n += 2;
$noc += 2;
} elseif (224 <= $t && $t < 239) {
$tn = 3;
$n += 3;
$noc += 2;
} elseif (240 <= $t && $t <= 247) {
$tn = 4;
$n += 4;
$noc += 2;
} elseif (248 <= $t && $t <= 251) {
$tn = 5;
$n += 5;
$noc += 2;
} elseif ($t == 252 || $t == 253) {
$tn = 6;
$n += 6;
$noc += 2;
} else {
$n ++;
}
if ($noc >= $cutLen)break;
}
if ($noc > $cutLen) $n -= $tn;
$nStr = substr ( $str, 0, $n );
} else {
for ($i = 0; $i < $cutLen - 1; $i ++) {
if (ord ( $str [$i] ) > 127) {
$nStr .= $str [$i] . $str [$i + 1];
$i ++;
} else {
$nStr .= $str [$i];
}
}
}
$str = $nStr . $oDot;
}
if ($addSlashes) $str = addslashes ( $str );
$str = htmlspecialchars_decode ( $str );
return trim ( $str );
}
//返回當前的毫秒時(shí)間戳
function msectime() {
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectime;
}
$wjname = md5(msectime()+$a);
$truefile = $templetdird.'/txt/'.$wjname.'.txt';
$fp = fopen($truefile, 'w') or die("Unable to open file!");
fwrite($fp, $newcontent);
echo "成功寫(xiě)入第".($w+1)."篇文章<br>";
fclose($fp);
php處理文本文件,去除空行
去除空行
$str = file_get_contents('a.txt');
$str = explode(PHP_EOL, $str); //分割為數組,每行為一個(gè)數組元素
$str = array_filter($str); //去除數組中的空元素
$str = implode(PHP_EOL,$str); //用換行符連結數組為字符串
file_put_contents('b.txt',$str);
PHP讀取目錄下的文本文件
header("content-type:text/html;charset=utf-8");
$templetdird = str_replace("\\", '/', dirname(__FILE__) ) .'/ccc';
$dh = dir($templetdird);
while($filename=$dh->read())
{
if(!preg_match("#\.txt#", $filename)) continue;
$filenames[]=$filename;
}
$file_arr = file($file); ###得到數組
file() 函數把整個(gè)文件讀入一個(gè)數組中。
數組中的每個(gè)元素都是文件中相應的一行,包括換行符在內。
//$fp1 = fopen($templetdird.'/'.$randsz[$a], 'r');
//$content = fread($fp1, filesize($templetdird.'/'.$randsz[$a]));
//fclose($fp1);
//$cont_arr = explode(PHP_EOL, $content);
$cont_arr =file($templetdird.'/'.$randsz[$a]); //比上面這么多步驟數組結果還少最后一個(gè)空元素