用PHP脚本自动把纯文本文件转换成Web页面

最近,我的一个老朋友向我打电话求助。他从事记者的职业有多年了,最近获得了重新出版他的很多早期专栏的权利。他希望把他的作品贴在Web上;但是他的专栏都是以纯文本文件的形式保存的,而且他既没有时间也不想去为了把它们转换成为Web页面而学习HTML的知识。由于我是他电话本里唯一一个精通计算机的人,所以他打电话给我看我是否能够帮帮他。

“让我来处理吧,”我说:“一个小时以后再给我打电话。”当然了,当他几个小时以后打电话过来,我已经为他预备好了解决的方法。这需要用到一点点PHP,而我收获了他没完没了的感谢和一箱红酒。

那么我在这一个小时里做了些什么呢?这就是本篇文章的内容。我将告诉你如何使用PHP来快速将纯ASCII文本完美地转换成为可读的HTML标记。

首先让我们来看一个我朋友希望转换的纯文本文件的例子:

Green for Mars!
John R. Doe

The idea of little green men from Mars, long a staple of science fiction, may soon turn out to be less fantasy and more fact.

Recent samples sent by the latest Mars exploration team indicate a high presence of chlorophyll in the atmosphere. Chlorophyll, you will recall, is what makes plants green. Its quite likely, therefore, that organisms on Mars will have, through continued exposure to the green stuff, developed a greenish tinge on their outer exoskeleton.

An interview with Dr. Rushel Bunter, the head of ASDAs Mars Colonization Project blah blah...

What does this mean for you? Well, it means blah blahblah...

Track follow-ups to this story online at http://www.mars-connect.dom/. To see pictures of the latest samples, log on to http://www.asdamcp.dom/galleries/220/

相当标准的文本:它有一个标题、一个署名和很多段的文字。把这篇文档转换成为HTML真正需要做的是使用HTML的分行和分段标记把原文的布局保留在Web页面上。非凡的标点符号需要被转换成为对应的HTML符号,超链接需要变得可以点击。

下面的PHP代码(列表A)就会完成上面所有的任务:

列表A

<?php
// set source file name and path
$source = "toi200686.txt";

// read raw text as array
$raw = file($source) or die("Cannot read file");

// retrieve first and second lines (title and author)
$slug = array_shift($raw);
$byline = array_shift($raw);

// join remaining data into string
$data = join(, $raw);

// replace special characters with HTML entities
// replace line breaks with <br />
$html = nl2br(htmlspecialchars($data));

// replace multiple spaces with single spaces
$html = preg_replace(/ss /, , $html);

阅读全文

PHP程序加速探索之代码优化

把握了PEAR::BenchMark,现在你已经知道如何测试你的代码,知道如何判定你的代码是快是慢,是哪一部份比较慢。那么接下来我要说的就是如何消灭或优化那部份慢的代码。

  这一点上我个人最主要的经验只有两点,一是消除错误的或低效的循环;二是优化数据库查询语句。其实还存在一些其它的优化细节,比如“str_replace比ereg_replace快”、“echo比print快”等等。这些我暂时都放在一边,稍后我会提到用缓存来对付过于频繁的IO。

  下面我们将三个功能相同,但程序写法不同的函数的效率(消耗的时间)进行对比。

  badloops.php

<?php
require_once(Benchmark/Iterate.php);
define(MAX_RUN,100);
$data = array(1, 2, 3, 4, 5);

doBenchmark(v1, $data);
doBenchmark(v2, $data);
doBenchmark(v3, $data);
function doBenchmark($functionName = null, $arr = null)
{
 reset($arr);
 $benchmark = new Benchmark_Iterate;
 $benchmark->run(MAX_RUN, $functionName, $arr);
 $result = $benchmark->get();
 echo <br>;
 printf("%s ran %d times where average exec time %.5f ms",$functionName,$result[iterations],$result[mean] * 1000);
}

function v1($myArray = null) {
 // 效率很差的循环
 for ($i =0; $i < sizeof($myArray); $i )
 {
  echo <!-- . $myArray[$i] . --> ;
 }
}

function v2($myArray = null) {
 // 效率略有提高
 $max = sizeof($myArray);
 for ($i =0; $i < $max ; $i )
 {
  echo <!-- . $myArray[$i] . --> ;
 }
}

function v3($myArray = null){
 //最佳效率
 echo "<!--", implode(" --> <!--", $myArray), " --> ";
}

?>

  程序输出的结果大概是这样的:

  v1 ran 100 times where average exec time 0.18400 ms
  v2 ran 100 times where average exec time 0.15500 ms
  v3 ran 100 times where average exec time 0.09100 ms

  可以看到,函数的执行时间变少,效率上升。

  函数v1有个很明显的错误,每一次循环的时间,都需要调用sizeof()函数来计算。函数v2则在循环外把$myArray数组的元素个数存到$max变量中,避免了每次循环都要计算数组的元素个数,所以效率提高了。函数v3的效率最高,利用了现成的函数,避免循环。

  这个例子只是给你一个感性的熟悉,明白什么是相对高效的代码。在实际开发中,我相信会有很多人会模模糊糊地写出很多低效率的代码。要把代码写得精炼而高效,恐怕需要时间去锤炼:-) 但这是另一个话题了,我们略过不谈。

  数据库应用基本上每个PHP程序都会用到,在实际开发中我发现最影响整个系统效率的就是数据库这部份。

阅读全文

用PHP正则表达式清除字符串的空白

我们经常会处理来自用户输入或从数据库中读取的数据,可能在你的字符串中有多余的空白或制表符,回车等。存储这些额外的字符是有点浪费空间的。

假如您想要去掉字符串开始和结束的空白可以使用PHP内部函数trim() 。但是, 我们经常想完全清除空白。需要把开始和结束的空白清除掉,将多个空白变为一个空白,使用一个规则来处理同样的类型的其它空白。

完成这些可以使用PHP的正则表达式来完成

下例可以去除额外Whitespace

<?php
$str = " This line containstliberal rn use of whitespace.nn";

// First remove the leading/trailing whitespace
//去掉开始和结束的空白
$str = trim($str);

// Now remove any doubled-up whitespace
//去掉跟随别的挤在一块的空白
$str = preg_replace(/s(?=s)/, , $str);

// Finally, replace any non-space whitespace, with a space
//最后,去掉非space 的空白,用一个空格代替
$str = preg_replace(/[nrt]/, , $str);

// Echo out: This line contains liberal use of whitespace.
echo "<pre>{$str}</pre>";
?>


上例一步一步的去掉所有的空白。首先我们使用trim()函数来去掉开始和结束的空白。然后,我们使用preg_replace() 去除重复的。s代表任何whitespace 。(?=) 表示向前查找 。它味着只匹配后面有和它本身相同字符的字符。所以这个正则表达式的意思是: "被whitespace 字符跟随的任何whitespace 字符。" 我们用空白来替换掉,这样也就去除了,留下的将是唯一的whitespace 字符。

最后, 我们使用另一个正则表达式[nrt]来查找任何残余的换行符(n), 回车(r), 或制表符(t) 。我们用一个空格来替换这些。

阅读全文

用PHP模拟POST来提交数据

利用php的socket编程来直接给接口发送数据来模拟post的操作。

<?
/***********************************************************
Name: POST 测试程序 Vesion: 1.0 Date: 2004-08-05 ************************************************************/
/ $flag = 0;
//要post的数据
$argv = array(
var1=>abc,
var2=>你好吗);
//构造要post的字符串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$params .= "&";
$flag = 1;
}
$params.= $key."="; $params.= urlencode($value);
$flag = 1;
}
$length = strlen($params);
//创建socket连接
$fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
//构造post请求的头
$header = "POST /mobile/try.php HTTP/1.1rn";
$header .= "Host:127.0.0.1rn";
$header .= "Referer:/mobile/sendpost.phprn";
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: ".$length."rn";
$header .= "Connection: Closernrn";
//添加post的字符串
$header .= $params."rn";
//发送post的数据
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
$line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
if ($inheader && ($line == "n" || $line == "rn")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp);
?>

阅读全文

使用PHP的Socket写的POP3类(一)

查看 POP3/SMTP 协议的时候想尝试一下自己写一个操作类,核心没啥,就是使用 fsockopen ,然后写入/接收数据,只实现了最核心的部分功能,当作是学习 Socket 操作的练手。其中参考了 RFC 2449和一个国外的简单Web邮件系统 Uebimiau 的部分代码,不过绝对没有抄他滴,HOHO,绝对原创。假如你喜欢,请收藏,随便修改,嗯,但是记得不要删除偶类里的声名,究竟偶也是辛辛劳苦写了好几天呐。
另外,欢迎自由发挥,改善或者修正这个类,希望能够为你所用。代码没有认真仔细的调试,发现bug请自己修正,HOHO!

<?php
/**
* 类名:SocketPOPClient
* 功能:POP3 协议客户端的基本操作类
* 作者:heiyeluren <http://blog.csdn.net/heiyeshuwu>
* 时间:2006-7-3
* 参考:RFC 2449, Uebimiau
* 授权:BSD License
*/

class SocketPOPClient
{
var $strMessage = ;
var $intErrorNum = 0;
var $bolDebug = false;

var $strEmail = ;
var $strPasswd = ;
var $strHost = ;
var $intPort = 110;
var $intConnSecond = 30;
var $intBuffSize = 8192;

var $resHandler = NULL;
var $bolIsLogin = false;
var $strRequest = ;
var $strResponse = ;
var $arrRequest = array();
var $arrResponse = array();


//---------------
// 基础操作
//---------------

//构造函数
function SocketPOP3Client($strLoginEmail, $strLoginPasswd, $strPopHost=, $intPort=)
{
$this->strEmail = trim(strtolower($strLoginEmail));
$this->strPasswd = trim($strLoginPasswd);
$this->strHost = trim(strtolower($strPopHost));

if ($this->strEmail== || $this->strPasswd==)
{
$this->setMessage(Email address or Passwd is empty, 1001);
return false;
}
if (!preg_match("/^[w-] (.[w-] )*@[w-] (.[w-] ) $/i", $this->strEmail))
{
$this->setMessage(Email address invalid, 1002);
return false;
}
if ($this->strHost==)
{
$this->strHost = substr(strrchr($this->strEmail, "@"), 1);
}
if ($intPort!=)
{
$this->intPort = $intPort;
}
$this->connectHost();
}

//连接服务器
function connectHost()
{
if ($this->bolDebug)
{
echo "Connection ".$this->strHost." ...rn";
}
if (!$this->getIsConnect())

阅读全文

网页快照功能

<?PHP
//====================================================
//        FileName:    snap.class.php
//        Summary:    网页快照类
//        Author:        millken(迷路林肯)
//        LastModifed:2007-06-29
//        copyright (c)2007 [email]millken@gmail.com[/email]
//====================================================
class snap{
    var $dir;
    var $log;
    var $contents;
    var $filename;
    var $host;
    var $name;
    var $data_ts;
    var $ttl;
    var http://pic4.phprm.com/2013/09/05/$url.jpg;
    var $ts;
    function snap(){
        $this->log = "New snap() object instantiated.<br />n";  
        $this->dir = dirname(__FILE__)."/";
    }
    function fetch($url="",$ttl=10){
        $this->log .= "--------------------------------<br />fetch() called<br />n";
        $this->log .= "url: ".$url."<br />n";
        $hosts = parse_url($url);
        $this->host = $hosts[scheme].://.$hosts[host]./;
        if (!$url) {
            $this->log .= "OOPS: You need to pass a URL!<br />";
            return false;
        }
        $this->ttl = $ttl;
        $this->url = $url;
        $this->name = md5($this->url);
        $this->filename = $this->dir.$this->name;
        $this->log .= "Filename: ".$this->filename."<br />";
        $this->getFile_ts();
        $this->file_get_content();

阅读全文

横向重复区域显示二法

方法一. 注重这里有一个预先定义的图片记录集rsmpic 要横向重复的就是图片,请根据你的情况改为你的记录集名称.整洁地将横向重复内容放在一个表格内


<table width="100" border="0" align="center" cellpadding="5" cellspacing="5">

<?php $startrw = 0;// 开始定义横向重复内容 这里设定为 3 行 3 列
$endrw = $HLooper1__index;
$numberColumns = 3;
$numrows = 3;
while(($numrows <> 0) AND (!$rsmpic->EOF))
{
$startrw = $endrw 1;
$endrw = $endrw $numberColumns;?>

<tr>
<?php While (($startrw <= $endrw) AND (!$rsmpic->EOF)) { //开始重复内容?>

<td>
<table width="78%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="../<?php echo $rsmpic->Fields(M_Path); ?>" border="0"></td>
</tr>
</table>

</td>

<?php
$startrw = $startrw 1;
$rsmpic->MoveNext();}//以上重复td内容 ?>

</tr>

<?php $numrows=$numrows-1;
Wend; } //重复内容结束 ?>
</table>


方法二. 将重复标记拖到<td>外(纵向重复是在<tr>外),加入一个变量, 每重复 1 次加 1, 假如%3等于0 echo 一个"<tr>" . 请仔细看




<table width="100%" border="1" cellspacing="1" cellpadding="5">
<tr>
<?php $str=0;// 开始定义重复函数?>
<?php do { //开始重复?>

<td>
<?php $str ;?>
<?php echo $str; ?>.<?php echo $row_Recordset1[NAME]; ?>
</td>
<?php if ($str%3==0)echo "<tr>"; ?>

<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); // 重复td内容?>
</tr>

</table>

阅读全文

PHP开发程序加速运行探索之慢代码优化方法

把握了PEAR::BenchMark,现在你已经知道如何测试你的代码,知道如何判定你的代码是快是慢,是哪一部份比较慢。那么接下来我要说的就是如何消灭或优化那部份慢的代码。

  这一点上我个人最主要的经验只有两点,一是消除错误的或低效的循环;二是优化数据库查询语句。其实还存在一些其它的优化细节,比如“str_replace比ereg_replace快”、“echo比print快”等等。这些我暂时都放在一边,稍后我会提到用缓存来对付过于频繁的IO。

  下面我们将三个功能相同,但程序写法不同的函数的效率(消耗的时间)进行对比。

  badloops.php

<?php
require_once(Benchmark/Iterate.php);
define(MAX_RUN,100);
$data = array(1, 2, 3, 4, 5);

doBenchmark(v1, $data);
doBenchmark(v2, $data);
doBenchmark(v3, $data);
function doBenchmark($functionName = null, $arr = null)
{
 reset($arr);
 $benchmark = new Benchmark_Iterate;
 $benchmark->run(MAX_RUN, $functionName, $arr);
 $result = $benchmark->get();
 echo <br>;
 printf("%s ran %d times where average exec time %.5f ms",$functionName,$result[iterations],$result[mean] * 1000);
}

function v1($myArray = null) {
 // 效率很差的循环
 for ($i =0; $i < sizeof($myArray); $i )
 {
  echo <!-- . $myArray[$i] . --> ;
 }
}


function v2($myArray = null) {
 // 效率略有提高
 $max = sizeof($myArray);
 for ($i =0; $i < $max ; $i )
 {
  echo <!-- . $myArray[$i] . --> ;
 }
}

function v3($myArray = null){
 //最佳效率
 echo "<!--", implode(" --> <!--", $myArray), " --> ";
}

?>


  程序输出的结果大概是这样的:

  v1 ran 100 times where average exec time 0.18400 ms
  v2 ran 100 times where average exec time 0.15500 ms
  v3 ran 100 times where average exec time 0.09100 ms

  可以看到,函数的执行时间变少,效率上升。

  函数v1有个很明显的错误,每一次循环的时间,都需要调用sizeof()函数来计算。

函数v2则在循环外把$myArray数组的元素个数存到$max变量中,避免了每次循环都要计算数组的元素个数,所以效率提高了。函数v3的效率最高,利用了现成的函数,避免循环。

  这个例子只是给你一个感性的熟悉,明白什么是相对高效的代码。在实际开发中,我相信会有很多人会模模糊糊地写出很多低效率的代码。要把代码写得精炼而高效,恐怕需要时间去锤炼:-) 但这是另一个话题了,我们略过不谈。

  数据库应用基本上每个PHP程序都会用到,在实际开发中我发现最影响整个系统效率的就是数据库这部份。至于数据库的优化和数据查询语句的优化,在此限于篇幅不具体讨论。

阅读全文

获取远程图片并把它保存到本地

<?php

//
// Function: 获取远程图片并把它保存到本地
//
//
// 确定您有把文件写入本地服务器的权限
//
//
// 变量说明:
// $url 是远程图片的完整URL地址,不能为空。
// $filename 是可选变量: 假如为空,本地文件名将基于时间和日期
// 自动生成.

function GrabImage($url,$filename="") {
if($url==""):return false;endif;

if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg"):return false;endif;
$filename=date("dMYHis").$ext;
}

ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);

$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);

return $filename;
}


$img=GrabImage("http://news.bbc.co.uk/images/_1978837_detector_ap100.jpg","");
if($img):echo <pre><img src=".$img."></pre>;
else:echo "false";
endif;

?>

阅读全文

flex+php在线拍照二

flex+php在线拍照

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%"  xmlns:ns1="*" backgroundGradientColors="[#ecf9ff, #ecf9ff]" height="100%"  xmlns:ns2="view.*" creationComplete="initApp()">
  <mx:Style>
        Alert{font-size:12px;}
    </mx:Style>
    <mx:Script>
        <![CDATA[
            import mx.events.CloseEvent;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.controls.Alert;
             import mx.managers.CursorManager;
            private static const DEFAULT_CAMERA_WIDTH:Number = 160; //摄像头显示宽度
            private static const DEFAULT_CAMERA_HEIGHT:Number = 120; //摄像头显示高度
            private static const DEFAULT_WEBSERVICE_URL:String = "http://localhost:1888/Web/TestWebService.asmx?WSDL"; //WebService地址
           
            private var m_camera:Camera; //定义一个摄像头
            private var m_localVideo:Video; //定义一个本地视频
            private var m_pictureBitmapData:BitmapData //定义视频截图
            private var pic_width:int;
            private var pic_height:int;
            //[Bindable]
            private var m_pictureData:String;
           
            private function initApp():void
            {
                t_btn_Shooting.enabled = false;
                t_ban_Save.enabled = false;
                initCamera();
               pic_height=m_camera.height;
               pic_width=m_camera.width;
            }
           
            //初始化摄像头
            private function initCamera():void
            {
                m_camera = Camera.getCamera();
                if(m_camera != null)
                {
                    m_camera.addEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
                   
                    m_camera.setMode(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT,30);
                    m_localVideo = new Video();
                    m_localVideo.width = DEFAULT_CAMERA_WIDTH;
                    m_localVideo.height = DEFAULT_CAMERA_HEIGHT;
                    m_localVideo.attachCamera(m_camera);
                    t_vd_Video.addChild(m_localVideo);
                }
                else
                {
                    Alert.show("没有找到摄像头,是否重新查找。","提示:",Alert.OK|Alert.NO,this,__InitCamera);
                    return;
                }
            }
           
            //拍照按钮事件,进行视频截图
            private function SnapshotPicture():void
            {
                m_pictureBitmapData = new BitmapData(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT);
                m_pictureBitmapData.draw(t_vd_Video,new Matrix());
               
                var m_pictureBitmap:Bitmap = new Bitmap(m_pictureBitmapData);
                t_img_Picture.addChild(m_pictureBitmap);
               
                t_panel_Picture.visible = true;
                t_ban_Save.enabled = true;
            }
           
            //保存按钮事件,保存视频截图
            //通过WebService保存
            private function SavePicture():void
            {
                m_pictureData = "";
                //m_pictureBitmapData.setPixel(1,1,6558750);
                //label1.text=m_pictureBitmapData.getPixel(1,1).toString();
               
                for(var i:int = 0; i < DEFAULT_CAMERA_WIDTH; i++)
                {
                    for(var j:int = 0; j < DEFAULT_CAMERA_HEIGHT; j++)
                    {
                        if(m_pictureData.length > 0)
                        {
                            m_pictureData += "," + m_pictureBitmapData.getPixel(i,j).toString();
                        }
                        else
                        {
                            m_pictureData = m_pictureBitmapData.getPixel(i,j).toString();
                        }
                    }
                }
                service.getOperation("createjpeg").send(pic_width,pic_height,m_pictureData);
               // t_ws_SavePicture.SavePicture.send();
              
            }
            internal function faultHandler(evt:FaultEvent):void{
   //labelresult.text="error";
   CursorManager.removeBusyCursor();
   Alert.show("保存出错","提示",Alert.YES,this);
   
  }
  internal function createImage(evt:ResultEvent):void{
      //dg_article.dataProvider=evt.result; 
      CursorManager.removeBusyCursor();
   Alert.show("保存成功","提示",Alert.YES,this);
   var date:Date=new Date();
   this.left.headerphoto.source="http://www.tiyi88.com/image/header/0.jpg?id="+date.getMilliseconds();
  }
            //检测摄像头权限事件
            private function __onCameraStatusHandler(event:StatusEvent):void
            {
                if(!m_camera.muted)
                {
                    t_btn_Shooting.enabled = true;
                }
                else
                {
                    Alert.show("无法链接到活动摄像头,是否重新检测。","提示:",Alert.OK|Alert.NO,this,__InitCamera);
                }
                m_camera.removeEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
            }
           
            //当摄像头不存在,或连接不正常时重新获取
            private function __InitCamera(event:CloseEvent):void
            {
                if(event.detail == Alert.OK)
                {
                    initApp();
                }
            }
        ]]>
    </mx:Script>
    <mx:RemoteObject id="service" fault="faultHandler(event)" showBusyCursor="true"
              source="image" destination="amfphp">
  <!--定义远程方法-->
  <mx:method name="createjpeg" result="createImage(event)"/>
  </mx:RemoteObject>
 <ns1:header x="137" y="10">
 </ns1:header>
 <ns1:left1 x="137" y="158" id="left">
 </ns1:left1> 
  <mx:Panel x="406" y="158" width="180" height="232" layout="absolute" title="视频拍照" fontSize="12">
        <mx:VideoDisplay id="t_vd_Video" width="160" height="146"/>
        <mx:ControlBar horizontalAlign="right">
            <mx:Button id="t_btn_Shooting" label="拍照" click="SnapshotPicture()"/>
        </mx:ControlBar>
    </mx:Panel>
    <mx:Panel id="t_panel_Picture" x="647" y="158" width="180" height="232" layout="absolute" title="拍照图片" fontSize="12" visible="false">
        <mx:Image id="t_img_Picture" x="0" y="0" width="160" height="144"/>
        <mx:ControlBar  horizontalAlign="right">
            <mx:Button id="t_ban_Save" label="保存" click="SavePicture()" />
        </mx:ControlBar>
    </mx:Panel>
</mx:Application>

阅读全文