php7x
1. php中編碼轉換問題
function uc2html($str) {
$ret = ' ';
for( $i=0; $i <strlen($str)/2; $i++ ) {
$charcode = ord($str[$i*2])+256*ord($str[$i*2+1]);
$ret .= iconv( "utf-8 ", "gb2312 ",u2utf8($charcode));
}
return $ret;
}
function u2utf8($c) {
$str= " ";
if ($c < 0x80) {
$str.=$c;
} else if ($c < 0x800) {
$str.=chr(0xC0 | $c> > 6);
$str.=chr(0x80 | $c & 0x3F);
} else if ($c < 0x10000) {
$str.=chr(0xE0 | $c> > 12);
$str.=chr(0x80 | $c> > 6 & 0x3F);
$str.=chr(0x80 | $c & 0x3F);
} else if ($c < 0x200000) {
$str.=chr(0xF0 | $c> > 18);
$str.=chr(0x80 | $c> > 12 & 0x3F);
$str.=chr(0x80 | $c> > 6 & 0x3F);
$str.=chr(0x80 | $c & 0x3F);
}
return $str;
}
如果你不是smarty的話 試試這個 如果是smarty的話 用下面的方法
<?php
/*
@Author: 蝸牛
@Blog: http://www.00562.com
@Note: 這個解決辦法是基於上面那個地址提到的方法,解決了中英文截取長度時出現亂碼的問題
*/
function smarty_modifier_truncate($string, $sublen = 80, $etc = '...', $break_words = false, $middle = false)
{
$start=0;
$code="UTF-8";
if($code == 'UTF-8')
{
//如果有中文則減去中文的個數
$cncount=cncount($string);
if($cncount>($sublen/2))
{
$sublen=ceil($sublen/2);
}
else
{
$sublen=$sublen-$cncount;
}
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
return join('', array_slice($t_string[0], $start, $sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i<$strlen; $i++)
{
if($i>=$start && $i<($start+$sublen))
{
if(ord(substr($string, $i, 1))>129)
{
$tmpstr.= substr($string, $i, 2);
}
else
{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
return $tmpstr;
}
}
function cncount($str)
{
$len=strlen($str);
$cncount=0;
for($i=0;$i<$len;$i++)
{
$temp_str=substr($str,$i,1);
if(ord($temp_str) > 127)
{
$cncount++;
}
}
return ceil($cncount/3);
}
?>
是可以的以上兩種方法 site:www.00562.com
2. 如何選擇php的版本
您好,很高興為您解答:
開發新項目:推薦使用PHP7的版本,他是一個趨勢,也是優化後的結晶,他的效率非常高。
維護老項目:推薦使用PHP5.2的版本,非常穩定!
使用開源項目/CMS之類的:查詢源碼支持的PHP版本,一般官網都有文檔,像DeDecms推薦使用PHP5.2,WP最新版本推薦PHP7
Laravel,Thinkphp5也支持PHP7了
3. php做一個乘法表
$a = array(1,2,3,4,5,6,7,8,9);
foreach ($a as $value)
{
foreach ($a as $v)
{
echo $value.'X'.$v.'='.$value*$v.' ';
}
echo '<br />';
}
4. 現在的PHP虛擬主機都支持PHP7嗎
您好,不是所有主機都支持PHP7.x,就目前來說,大概有10~20%的服務商支持PHP7系,硅雲目前已經支持到了最新的PHP7.3。
5. 這個是什麼編碼 開頭一共7位 PHP用什麼函數轉換成中文
這是通用編碼,所有的編碼都適用,iconv函數可以轉成中文,只要第二個參數是"utf-8"就行!
6. 什麼是標量類型 php中的
數據類型分為:標量數據類型,復合數據類型,特殊數據類型1.標量數據類型:是數據結構中最基本單元,只能儲存一個數據,包括boolean,string,integer,float1.1string類型:定義字元串與三種方式:單引號(『)、雙引號(「)、界定符(<<<)單引號和雙引號是常使用定義方式,區別是雙引號中包含的變數會自動被替換成實際數值,而單引號包含的變數則按普通字元串輸出。<?php$i="welcome to network1024";echo '$i'; //將輸出$iecho "$i"; //輸出welcome to network1024?> 轉義字元:
序列 含義\n 換行\r 回車\t 水平製表符\\ 反斜線\\$ 符號$\』 符號』\」 符號」\[0-7]{1,3} 此正則表達式序列匹配一個用八進制符號表示的符號\x[0-9A-Fa-f]{1-2} 此正則表達式序列匹配一個用十六進制符號表示的符號注意:\n和\r在windows系統下沒區別,都可當回車符;linux下\n表示游標回到行首,仍在本行,\r則換到下一行,卻不會回到行首。<?php$i=<<<network1024 //必須另起一行welcome to network1024network1024;echo "$i"; //輸出welcome to network1024?>1.2integer類型:32位系統范圍:-2147483648~2147483647,如果超出范圍發生整數溢出,當float處理,返回float類型;表示方式:十進制:123;八進制:0123;十六進制:0x123;1.3float類型:32位系統范圍:1.7E-308~1.7E+308表示方式:標准格式:3.141592654;科學計數法試:3141592654E-9注意:浮點數數值只是一個近似值,所以盡量避免浮點數間比較大小,因為最後的結果往往不準確。 2復合數據類型:包括數組array和對象object數組類型:是一個數據集合,可以包括多種數據:標量數據、數組、對象、資源、及PHP中支持的其他語法結構。數組中每個數據成為一個元素,元素包括索引(鍵名)和值兩部分。元素索引只能有數字或字元串組成。元素值可以是多種數據類型。數組索引自動編號從0開始語法格式:$array=(「value1」,」value2」……)或$array[key]=」value」或$array(key1=>value1,key2=value2……)<?php$network1024=array(1=>"how",2=>2,'are'=>"you");echo $network1024[2]; //輸出2echo $network1024[are]; //輸出you?>聲明數組後,數組中的元素個數可以自由更改。只要給數組賦值,數組就會自動增加長度。 3特殊類型:包括空值NULL和資源resourceresource:資源是由專門的函數來建立和使用的,它是一種特殊的數據類型,並由程序員分配。在使用資源時,要及時釋放不需要的資源。如果忘記釋放資源,系統自動啟動垃圾回收機制,避免內存消耗殆盡。NULL:表示變數沒有值。NULL不區分大小寫,null和NULL都是一樣。為NULL的情況:被賦為null;尚未被賦值;通過函數unset()而被銷毀。 4數據類型轉換:(type)value類型關鍵字 類型轉換 類型關鍵字 類型轉換(int),(integer) 轉換成整形 (array) 轉換成數組(float),(double),(real) 轉換成浮點型 (object) 轉換成對象(bool),(boolean) 轉換成浮點型 (string) 轉換成浮點型注意:轉換為boolean:null、0、未賦值的變數或數組會轉換為false,其他為true轉化為integer:boolean:false為0,true為1 float:小數部分被捨去 string:以數字開頭則截取到非數字位,否則輸出0 通過函數bool settpye(mixed var , string type)var為指定變數;type為要轉換的類型boolean/float/integer/string/array/null/objectsettype會改變變數自身類型
7. php7 linux上使用 call_user_func_array 報錯
php __call()與call_user_func_array()理解 1. mixed __call ( string name, array arguments )The magic method __call() allows to capture invocation of non existing methods. That way __call() can be used to implement user defined method handling that depends on the name of the actual method being called. This is for instance useful for proxy implementations. The arguments that were passed in the function will be defined as an array in the $arguments parameter. The value returned from the __call() method will be returned to the caller of the method. 譯文: 這個魔術方法允許用戶調用類中不存在的方法,它用於實現那些 依賴於在被調用時的真正方法名的方法. 典型的例子是用來實現代理. 方法的參數$arguments是一個數組 ,__call()的返回值返回給方法調用者白話文: 這個方法主要是用來實現動態方法調用, 如果再一個類定義了__call()這個方法, 當用戶調用這個類的一個不存在的方法時,他可以使用調用的那個不存在的方法的方法名和參數做出用戶定義在__call()方法體內的相應操作,此時__call()方法的參數就是被調用的那個不存在的方法的方法名和參數例子<?phpclass Person{function talk( $sound ){echo $sound;}function __call( $method , $args ){echo 'you call method ' . $method . '
';echo 'and the arguments are
';var_mp( $args );}}$person = new Person();$person->test( 1 , TRUE );?>程序輸出引用you call method testand the arguments are array 0 => int 1 1 => boolean true2. mixed call_user_func_array ( callback function, array param_arr )Call a user defined function with the parameters in param_arr. 參數functionThe function to be called. param_arrThe parameters to be passed to the function, as an indexed array. 返回值Returns the function result, or FALSE on error. 此方法可以通過傳入類名,類中得方法名和方法參數達到動態調用方法的效果例子<?php class Person{function talk( $sound ){echo $sound;}function __call( $method , $args ){echo 'you call method ' . $method . '
';echo 'and the arguments are
';var_mp( $args );}} $person = new Person();call_user_func_array( array( $person , 'talk' ) , array( 'hello' ) );?>程序輸出引用hello兩個方法共用,實現代理模型 class Person{function talk( $sound ){echo $sound;}function __call( $method , $args ){echo 'you call method ' . $method . '
';echo 'and the arguments are
';var_mp( $args );}}class PersonProxy{private $person;function __construct(){$this->person = new Person();}function __call( $method , $args ){call_user_func_array( array( $this->person , $method ) , $args );}}$person_proxy = new PersonProxy(); $person_proxy->talk( 'thank you' );程序輸出引用thank yo
8. php有什麼好的是什麼
PHP的簡介:
PHP是一種【後端編程語言】,其主要用於後台數據處理~其英文名稱就是(Hypertext Preprocessor)。
【好處】語法吸收了C語言、Java和Perl的特點,【利於學習,使用廣泛,主要適用於Web開發領域】。PHP 獨特的語法混合了C、Java、Perl以及PHP自創的語法。它可以比CGI或者Perl更快速地執行動態網頁。用PHP做出的動態頁面與其他的編程語言相比,PHP是將程序嵌入到HTML(標准通用標記語言下的一個應用)文檔中去執行,【執行效率比完全生成HTML標記的CGI要高許多;PHP還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。】
官網:PHP
PHP的具體好處:
庫多,有zip庫,mysqli庫,SQL庫,等等
語法簡介,同C++同C同Java基本一致~方便學習
安全~因為PHP的源碼是開源的,所以大家都紛紛研究加密程序~
方便使用,PHP是跨平台的一門語言~
環境容易安裝,例如macOS,系統自帶PHP
【圖:PHP的庫的數量】
【Facebook】
9. 如何去掉PHP的 X
我們在使用抓包軟體或者CURL的時候可以看到head頭信息,我們可以執行下面命令
幫助
1
curl -I 需要測試的域名
QQ截圖20140116190956
其中就包括X-Powered-By: PHP/5.3.17信息,連版本號都暴露出來了,萬一此PHP版本有問題極大的影響安全性。
建議大家去掉X-Powered-By:php/5.3.17 ,我們需要編輯php.ini 文件,使用vim 編輯
幫助
1
vim /usr/local/php/etc/php.ini
找到 expose_php 參數,發現 expose_php = On ,我們把它設置為expose_php = Off,重啟nginx,發現X-Powered-By:php/5.3.17 消失了。
此條目發表在php語言分類目錄。將固定鏈接加入收藏夾。
← mysql如何批量插入數據