新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
php中txt怎么轉(zhuǎn)成數(shù)組
本教程操作環(huán)境:windows10系統(tǒng)、PHP7.1版、DELL G3電腦

肇東ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
file_get_contents() 把整個文件讀入一個字符串中。
該函數(shù)是用于把文件的內(nèi)容讀入到一個字符串中的首選方法。如果服務(wù)器操作系統(tǒng)支持,還會使用內(nèi)存映射技術(shù)來增強性能。
語法
file_get_contents(path,include_path,context,start,max_length)
explode() 函數(shù)使用一個字符串分割另一個字符串,并返回由字符串組成的數(shù)組。
語法
explode(separator,string,limit)
示例如下:
文本內(nèi)容示例:
我愛你 中國 我愛你 NO.1 TOP1 123456789 123456
代碼:
/**
* 讀取txt文件內(nèi)容轉(zhuǎn)換成數(shù)組
*/
$file = 'data.txt';
if(file_exists($file)) {
$content = file_get_contents($file); //文件內(nèi)容讀入字符串
if (empty($content)) {
echo "文件內(nèi)容為空";
} else {
$content = mb_convert_encoding($content, 'UTF-8', 'ASCII,UTF-8,GB2312,GBK,BIG5'); //轉(zhuǎn)換字符編碼為utf-8
$array = explode("\r\n", $content); //轉(zhuǎn)換成數(shù)組
$array = array_filter($array); // 去空
$array = array_unique($array); // 去重
print_r(json_encode($array));
}
}else{
echo "文件不存在";
}
執(zhí)行結(jié)果:
當(dāng)前標(biāo)題:php中txt怎么轉(zhuǎn)成數(shù)組
文章網(wǎng)址:http://fisionsoft.com.cn/article/coghpcj.html


咨詢
建站咨詢
