当前位置:首页 » 编程语言 » 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方式获取内容。

热点内容
db2新建数据库 发布:2024-09-08 08:10:19 浏览:170
频率计源码 发布:2024-09-08 07:40:26 浏览:778
奥迪a6哪个配置带后排加热 发布:2024-09-08 07:06:32 浏览:100
linux修改apache端口 发布:2024-09-08 07:05:49 浏览:208
有多少个不同的密码子 发布:2024-09-08 07:00:46 浏览:566
linux搭建mysql服务器配置 发布:2024-09-08 06:50:02 浏览:995
加上www不能访问 发布:2024-09-08 06:39:52 浏览:811
银行支付密码器怎么用 发布:2024-09-08 06:39:52 浏览:513
苹果手机清理浏览器缓存怎么清理缓存 发布:2024-09-08 06:31:32 浏览:554
云服务器的优点与缺点 发布:2024-09-08 06:30:34 浏览:734