PHP自定義函式實現防止中文出現亂碼

2021-08-28 01:48:18 字數 2276 閱讀 8423

一 **

function.php

phpfunction

chinesesubstr

($str

,$start

,$len

)else

$tmpstr

.=substr

($str,$i

,1);

}

return

$tmpstr

;}

?>

index.php

rel

="stylesheet"

type

="text/css"

href

="style.css"

>

width

="160"

border

="0"

align

="center"

cellpadding

="0"

cellspacing

="0"

>

width

="165"

height

="32"

>src

="images/tell_top.gif"

width

="165"

height

="32"

border

="0"

>

height

="52"

background

="images/tell_center.gif"

>

phpinclude_once

("function.php"

);$news

="中小學因霧霾放假!"

;$i=1

;do

?>

>

height

="5"

>

height

="5"

background

="images/back_point_write.gif"

>

php$i

++;}

while($i

<=

5);?>

width

="165"

height

="12"

>src

="images/tell_bottom.gif"

width

="165"

height

="12"

>

二 執行效果

三 **說明

1、為了保持整個頁面合理布局,經常需要對一些較長的字元進行部分輸出,但由於漢字占有兩個字元,如果擷取位置不當就可能導致擷取的字串尾出現亂碼。

2、本**實現功能:建立乙個自定義函式,用於實現遮蔽中文亂碼的輸出,將該函式封裝在乙個function.php中,然後應用include_once語句引用這個檔案,再通過echo語句輸出新聞主題資訊,擷取前16個 字元,並應用自定義函式遮蔽中文亂碼。

PHP如何實現自定義函式來翻轉中文

對於php語言來說,說到字串翻轉大家首先想到應該就是strrev 這個函式了,定義和用法 strrev 函式反轉字串。語法 strrev string 引數 描述 string 必需。規定要反轉的字串。例如 echo strrev hello world 輸出結果為 dlrow olleh strr...

php防止sql注入的自定義函式

1.函式的構建 function inject check sql str function verify id id null 是否為空判斷 elseif inject check id 注射判斷 elseif is numeric id 數字判斷 id intval id 整型化 return ...

PHP自定義函式

使用者自定義函式也稱自定義函式,它們不是php提供的,是由程式設計師建立的.由於自己建立了這樣的函式,所以就可以完全控制這些函式.因此可以讓乙個函式完全按照自己希望的方式執行.1,申明函式 在php中,定義函式的方法同其他程式語言幾乎一樣.下面是php申明函式的語法結構 function funct...