當前位置:首頁 » 編程語言 » php取文本

php取文本

發布時間: 2024-02-25 12:27:44

1. php如何讀取文本指定的內容

php讀取文件內容:
-----第一種方法-----fread()--------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定讀取大小,這里把整個文件內容讀取出來
echo $str = str_replace("\r\n","<br />",$str);
}
?>

--------第二種方法------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//將整個文件內容讀入到一個字元串中
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
-----第三種方法------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次讀取 1024 位元組
while(!feof($fp)){//循環讀取,直至讀取完整個文件
$str .= fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
-------第四種方法--------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行讀取文件內容
echo $file_arr[$i]."<br />";
}
/*
foreach($file_arr as $value){
echo $value."<br />";
}*/
}
?>
----第五種方法--------------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);//逐行讀取。如果fgets不寫length參數,默認是讀取1k。
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>

2. php如何獲取txt文本指定行的指定數據

如果直接使用file_get_contents來讀取文件,那麼在文件很大的時候會很占內容,比如這個文件有1GB的時候。
這個時候使用傳統的文件操作方式就好的多,因為是查找嘛,逐行讀取匹配應該也是可以的,下面是我的一個建議,不知道是否滿足你的要求,可以看下:
//
需要查找的內容
$search
=
'bcd';
//
打開文件
$res
=
fopen('a.txt',
'r');
while
($line
=
fgets($res,
1024))
{
//
根據規則查找
if
(strpos($line,
$search)
===
0)
{
//
根據既定規則取得需要的數據
echo
substr($line,
4,
-1);
//
這里就是你想得到的
break;
}
}
//
關閉文件
fclose($res);

3. php讀取文本文件內容~

示例代碼1: 用file_get_contents 以get方式獲取內容
代碼如下:
<?php
$url='';
$html=file_get_contents($url);
//print_r($http_response_header);
ec($html);
printhr();
printarr($http_response_header);
printhr();
?>

示例代碼2: 用fopen打開url, 以get方式獲取內容
代碼如下:
<?
$fp=fopen($url,'r');
printarr(stream_get_meta_data($fp));
printhr();
while(!feof($fp)){
$result.=fgets($fp,1024);
}
echo"url body:$result";
printhr();
fclose($fp);
?>

示例代碼3:用file_get_contents函數,以post方式獲取url
代碼如下:
<?php
$data=array('foo'=>'bar');
$data=http_build_query($data);
$opts=array(
'http'=>array(
'method'=>'POST',
'header'=>"Content-type: application/x-www-form-urlencodedrn".
"Content-Length: ".strlen($data)."rn",
'content'=>$data
),
);
$context=stream_context_create($opts);
$html=file_get_contents('',false,$context);
echo$html;
?>

示例代碼4:用fsockopen函數打開url,以get方式獲取完整的數據,包括header和body
代碼如下:
<?
functionget_url($url,$cookie=false){
$url=parse_url($url);
$query=$url[path]."?".$url[query];
ec("Query:".$query);
$fp=fsockopen($url[host],$url[port]?$url[port]:80,$errno,$errstr,30);
if(!$fp){
returnfalse;
}else{
$request="GET$queryHTTP/1.1rn";
$request.="Host:$url[host]rn";
$request.="Connection: Closern";
if($cookie)$request.="Cookie:$cookien";
$request.="rn";
fwrite($fp,$request);
while(!@feof($fp)){
$result.=@fgets($fp,1024);
}
fclose($fp);
return$result;
}
}
//獲取url的html部分,去掉header
functionGetUrlHTML($url,$cookie=false){
$rowdata=get_url($url,$cookie);
if($rowdata)
{
$body=stristr($rowdata,"rnrn");
$body=substr($body,4,strlen($body));
return$body;
}
returnfalse;
}
?>

示例代碼5:用fsockopen函數打開url,以POST方式獲取完整的數據,包括header和body
代碼如下:
<?
functionHTTP_Post($URL,$data,$cookie,$referrer=""){
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="")// if not given use this script. as referrer
$referrer="111";
// making string from $data
foreach($dataas$key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;
// building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1n";
$request.="Host: ".$URL_Info["host"]."n";
$request.="Referer:$referern";
$request.="Content-type: application/x-www-form-urlencodedn";
$request.="Content-length: ".strlen($data_string)."n";
$request.="Connection: closen";
$request.="Cookie:$cookien";
$request.="n";
$request.=$data_string."n";
$fp=fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp,$request);
while(!feof($fp)){
$result.=fgets($fp,1024);
}
fclose($fp);
return$result;
}
printhr();
?>

示例代碼6:使用curl庫,使用curl庫之前,你可能需要查看一下php.ini,查看是否已經打開了curl擴展
代碼如下:
<?
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, '');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>

關於curl庫:
curl官方網站
curl 是使用URL語法的傳送文件工具,支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。curl 支持SSL證書、HTTP POST、HTTP PUT 、FTP 上傳,kerberos、基於HTT格式的上傳、代理、cookie、用戶+口令證明、文件傳送恢復、http代理通道和大量其他有用的技巧
代碼如下:
<?
functionprintarr(array$arr)
{
echo"<br> Row field count: ".count($arr)."<br>";
foreach($arras$key=>$value)
{
echo"$key=$value <br>";
}
}
?>

4. PHP如何從文本中提取指定行數內容

PHP如何從文本中提取指定行數內容?在php中,通過fopen()方法打開文件,在while中使用fgets()方法獲取每行數據,每讀到一行,就使用標識記錄一次,通過累計記錄數計算出文件的行數。下面介紹實現的過程。
方法/步驟分步閱讀
1
/7
新建一個php文件,命名為handle.php,用於講解PHP怎麼獲取文件的行數。
2
/7
新建一個文本文件,命名為test.txt,在裡面輸入四行數據,分別是aaa,bbb,ccc,ddd。
3
/7
在handle.php文件里,使用fopen方法以只讀方式打開test.txt文件,代碼如下:
4
/7
在handle.php文件里,創建一個初始變數i,用於保存文件內容的行數。
5
/7
通過while()語句,使用fgets方法從文件指針中讀取一行,每讀取一行,變數i自加一,直到到達文件末尾停止while的執行。
註:!feof($handle),函數檢測是否已到達文件末尾。
6
/7
最後,使用echo輸出文件的行數,並通過fclose關閉文件資源。代碼如下:
7
/7
在瀏覽器執行handle.php文件,查看輸出的行數,執行的結果為4行。
內容僅供參考並受版權保護

5. php怎麼讀取txt文本內容存入mysql資料庫

這個要看你的txt 裡面是不是按資料庫欄位方式寫的如果是就好辦,我是這樣做,我用txt添加的是郵件地址
每行只要求一個地址

//上傳txt文本
if($_FILES['text']['name']){
$path='../upload';
if(!file_exists($path)){
mkdir($path);
}
if(!is_dir($path)){
mkdir($path);
}
$p=strrchr($_FILES['text']['name'],'.');
if(preg_match("/txt/",$p)){
$file=$path.'/'.date('Ymd').time().$p;
move_uploaded_file($_FILES['text']['tmp_name'],$file);

$get=fopen($file,'r');
$j=0;
while (!feof($get)){ //循環讀取每一行
$row=fgets($get);
$row=str_replace(' ','',$row);
$rowa=preg_match("/\@/",$row);
$sql="INSERT INTO `address`(`address`,`timees`,`data`)VALUES('".$rowa."','0',1)";
$db->guery($sql);
$j++;
}

}
echo"<script>alert('已經添加$j條');history.back();</script$amp;>quot;$;
}
}else{
echo"<script>alert('選擇正確添加方式 ');history.back();</script$amp;>quot;$;
}
fclose($get);

6. PHP中如何獲取文本框的值

有$_GET 或者 $_POST
代碼如下 :
<form action='' method='post'>
文本框:<input type='text' name='text'>
<input type='submit' value='提交',name='sub'>
</form>
<?php
if(!empty($_POST['sub'])){
echo $_POST['text'];

}

?>
如果是GET 就換成GET

7. PHP怎麼獲取裡面的內容

1、用file_get_contents,以get方式獲取內容。

熱點內容
安卓用什麼和電腦傳圖片 發布:2024-07-27 09:02:07 瀏覽:288
存儲過程就是 發布:2024-07-27 08:56:51 瀏覽:131
c語言高級試題 發布:2024-07-27 08:48:30 瀏覽:282
ip伺服器世界上有幾台 發布:2024-07-27 08:46:18 瀏覽:394
金立手機怎麼清理緩存 發布:2024-07-27 08:38:50 瀏覽:311
iphone文件夾不顯示 發布:2024-07-27 08:18:05 瀏覽:774
y510p固態硬碟做緩存 發布:2024-07-27 07:59:34 瀏覽:128
奶塊為什麼進伺服器會排隊 發布:2024-07-27 07:57:15 瀏覽:691
資料庫表標識 發布:2024-07-27 07:50:00 瀏覽:923
python元組個數 發布:2024-07-27 07:49:23 瀏覽:236