php 读取文件内容[file_get_contents]

 更新时间:2016年11月25日 15:13  点击:1722
下面是一个简单的函数,我们是用php file_get_contents函数来读取文件mail_room.html的内容哦,好了费话不说多了我们来看看吧。


 function GetContent($type){
  if( $type )
  {
   if(file_exists('./mail_room.html') )
   {
    $content = file_get_contents( './mail_room.html');
   }
   else
   {
    ShowMsg('file can' read fail ');
   }
  }
  else
  {
   if( file_exists( './mail_person.html') )
   {
    $content = file_get_contents( './mail_person.html');
   }
   else
   {
    ShowMsg('person file read fail!');
   }
   
  }
  return $content;
 }

本站原创转注明:www.111cn.net/phper/php.html

本文章要讲的是关于php的substr函数与自己写了一个中英文截取函数哦,关于首先我们来看看substr这个函数的使用方法吧。

substr实例

$content ='i love you www.111cn.net';

 $temp = substr($content,4);

echo $temp;

结果:

love you www.111cn.net' //

下面来看看从右边取函数。


 $temp = substr($temp,0,-4);

结果为:

i love www.111c

好了,下面我们再来看中文截函数吧。

function MooCutstr($string, $length, $dot = ' ...') {
 global $charset;

 if(strlen($string) <= $length) {
  return $string;
 }
 $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
 $strcut = '';
 if(strtolower($charset) == 'utf-8') {
  $n = $tn = $noc = 0;
  while($n < strlen($string)) {
   $t = ord($string[$n]);
   if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
    $tn = 1; $n++; $noc++;
   } elseif (194 <= $t && $t <= 223) {
    $tn = 2; $n += 2; $noc += 2;
   } elseif (224 <= $t && $t < 239) {
    $tn = 3; $n += 3; $noc += 2;
   } elseif (240 <= $t && $t <= 247) {
    $tn = 4; $n += 4; $noc += 2;
   } elseif (248 <= $t && $t <= 251) {
    $tn = 5; $n += 5; $noc += 2;
   } elseif ($t == 252 || $t == 253) {
    $tn = 6; $n += 6; $noc += 2;
   } else {
    $n++;
   }
   if($noc >= $length) {
    break;
   }
  }
  if($noc > $length) {
   $n -= $tn;
  }
  $strcut = substr($string, 0, $n);
 } else {
  for($i = 0; $i < $length; $i++) {
   $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
  }
 }
 //$strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);

 return $strcut.$dot;
}

php substr它不能自动识别中英,所以很多朋友在用substr读取中文与英的字符串会有时会出现乱码了,使用第二种方法就OK了。

下面是一个简单的php 编程规范化写法哦,好了下面看看吧,对初学者应该比较有有用

<?php
 /*
 Y:09.05.05
 A:jimmy
 Login
 */

程序开始要有说明


 include("Inc/Conn.php");
 include("Inc/function.php");

公共文件全部include进来


 $integrals = Get_value('integrals',0);


 $uid = Get_value('id',0);

取得数据写专业的函数处理
 if( !is_numeric( $integrals ) || !is_numeric( $uid ) )
 {
  ShowMsg('{"result":"false"}');
 }
 else
 {
  $sql = "update oy_use""";
  if( mysql_query( $sql ) )
  {
   ShowMsg('{"result":"ture"}');
  }
  else
  {
   ShowMsg('{"result":"false"}');
  }
 }

php里面的语句单独放一行
?>

在php有一些特殊的函数,php 构造函数与析构函数[__construct __destruct()]哦,他在在类class中的作用是初始化与销毁变量下面我们来看看实例以\

在php有一些特殊的函数,php 构造函数与析构函数[__construct __destruct()]哦,他在在类class中的作用是初始化与销毁变量下面我们来看看实例以

class db

{

  function __construct()
  {            
      
   $this->mConnId=mysql_connect ($this->DbHost,$this->DbUser,$this->DbPwd);//建立连接

            mysql_select_db($this->DbName, $this->mConnId);    //选择数据库

            mysql_query("set names 'gbk'");//设置数据库编码为GBK

        }
       
        //__destruct:析构函数,断开连接

 function __destruct()
  {
            mysql_close($this->mConnId); //此处还有问题......

        }
 }

这时我们在用时就不需要考虑数据连接与关闭了,只要$aa = new db();就OK了。

php curl_init() 实例

function ip($date){
 $rul="http://111cn.net/s?ie=gbk&sr=&z=&cl=3&f=8&wd=$date&ct=0&tn=downreg";
 $ch = curl_init();
 $timeout = 5;
 curl_setopt ($ch, CURLOPT_URL, "$rul");
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 $contents = curl_exec($ch);
 curl_close($ch);
 preg_match_all("/&nbsp;&nbsp;&nbsp;&nbsp;来自:(.*?)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;此数据由/is",$contents,$address);
 if(empty($address[1][0]))$address[1][0]='未知';
 return $address[1][0];
}

[!--infotagslink--]

相关文章

  • php 中file_get_contents超时问题的解决方法

    file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25
  • php file_get_contents 设置代理抓取页面示例

    file_get_contents函数在php中可以直接打开本地文件也可以直接抓取远程服务器文件,如果简单的采集我们可以使用file_get_contents直接来操作,如果有防采集我们可能需要...2016-11-25
  • php报错file_get_contents(): php_network_getaddresses问题

    本文章来为各位介绍一篇关于file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known...错误解决办法。 昨天,服务器的DN...2016-11-25
  • PHP file_get_contents设置超时处理方法

    file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
  • file_get_contents()获取https出现这个错误Unable to find the wrapper “https”

    下面我们来看一篇关于file_get_contents()获取https出现这个错误Unable to find the wrapper “https”问题的解决办法. file_get_contents()获取https出现这个错...2016-11-25
  • Shell逐行读取文件的4种方法

    这篇文章主要介绍了Shell逐行读取文件的4种方法,本文介绍了while循环法、重定向法、管道法、文件描述符法等一些方法,需要的朋友可以参考下...2020-07-11
  • file_put_contents并发性问题解决方案整理

    在使用file_put_contents时会碰到并发性问题了,对于这个问题我们有多种解决方案了,其实锁是小编比较喜欢的解决办法了,当然也有其它办法,具体如下。 解决 办法一,fil...2016-11-25
  • php提示Warning: file_get_contents(): couldn’t resolve

    在使用file_get_contents函数获取远程文件时提示Warning: file_get_contents(): couldn’t resolve错误了,这个我们可以看出是dns的问题,解决办法也简单。 今天在...2016-11-25
  • C# StreamReader类实现读取文件的方法

    这篇文章主要介绍了C# StreamReader类实现读取文件的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-22
  • c#读取文件详谈

    你平时是怎么读取文件的?使用流读取。是的没错,C#给我们提供了非常强大的类库(又一次吹捧了.NET一番)...2020-06-25
  • php中file_get_contents和curl_get_contents介绍

    php中file_get_contents和curl_get_contents介绍 有需要的朋友可参考一下。 分享一个实际在用的函数: file_get_contents() 函数是用于将文件的内容读入到一个字符...2016-11-25
  • file_get_contents不能获取带端口的网址

    本文章来给各位同学介绍file_get_contents不能获取带端口的网址解决办法,有需要了解的同学可参考。 先们来了解file_get_contents() 函数,官方介绍说它是把整个...2016-11-25
  • 浅谈Golang是如何读取文件内容的(7种)

    这篇文章主要介绍了浅谈Golang是如何读取文件内容的,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-05-28
  • centos下file_put_contents()无法写入文件的原因及解决方法

    下面小编就为大家带来一篇centos下file_put_contents()无法写入文件的原因及解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2017-04-03
  • php读取文件的方法

    PHP读写文件,就如同ASP中使用FSO进行文件的读写操作。当然在ASP中FSO仅对于运行当前程序的服务器磁盘上文件进行读写(很明显就是需要获得物...2016-11-25
  • php 读取文件函数

     1、用file_get_contents或者fopen、file、readfile等函数读取url的时候,会创建一个名为$http_response_header的变量来保存http响应的报头,使用fopen等函数打开的数据...2016-11-25
  • PHP读取文件内容并输出显示

    一个小的应用要用到txt文件来读取文件的内容之后再显示出来文件里面的内容了,下面来给各位分个例子,希望能帮助到各位哦。 例子 代码如下 复制代码 ...2016-11-25
  • php file_put_contents 生成文件

    <html> <head> <title>test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="http://ajax...2016-11-25
  • php curl file_get_contents post方式获取数据

    下面我们在这里来为各位介绍一篇关于php curl file_get_contents post方式获取数据例子,希望文章能够帮助到各位朋友. curl post,file_get_contents post,curl fi...2016-11-25
  • PHP中file_put_contents写入文件的优点

    file_put_contents写入文件在我看到的phper中很少用到了,但小编以前做flash接受数据时就用到了file_put_contents函数了,下面我们来看看file_put_contents写入文件的优...2016-11-25