• 项目网址: http://sourceforge.net/projects/simplehtmldom/ 

    特色: 
    1.只支援PHP5以上
    2.可以分析不严谨(invalid)的HTML. 
    3.支援简单的CSS Selector. 
    4.简单的DOM操作
    5.会维持HTML中的原始格式.

    一些范例:

    <?php

    //示范如何读取HTML元素 

    include(’html_dom_parser.php’); 

    //产生DOM物件 

    $dom = file_get_dom('[url]http://www.google.com/[/url]'); 

    //找出所有网页连结 

    $result = $dom->find(’a); 

    foreach($result as $v) {echo $v->href . '<br>';} 

    //找出所有网页图片 

    $result = $dom->find(’img’); 

    foreach($result as $v) {echo $v->src . '<br>';} 

    //找出所有网页中所有id=gbar的div标签 

    $result = $dom->find(’div#gbar’); 

    foreach($result as $v) {echo $v->innertext . '<br>';} 

    //找出所有网页中所有calss=gb1的span标签 

    $result = $dom->find(’span.gb1′); 

    foreach($result as $v) {echo $v->outertext . '<br>';} 

    //找出所有网页中所有align=center的'td标签 

    $result = $dom->find(’td[align=center]‘); 

    foreach($result as $v) {echo $v->outertext . '<br>';} 

    ?> 

     
    <?php 

    //示范如何修改HTML元素 

    include(’html_dom_parser.php’); 

    //产生DOM物件 

    $dom = file_get_dom('[url]http://www.google.com/[/url]'); 

    //移除网页中所有图片 

    $ret = $dom->find(’img’); 

    foreach($ret as $v) {$v->outertext = ”;} 

    //修改网页中所有input标签 

    $ret = $dom->find(’input’); 

    foreach($ret as $v) {$v->outertext = '[INPUT]';} 

    //显示修改后的网页 

    echo $dom->save(); 

    ?>

  • 转自:http://www.csask.com/

    Memcache函数库是在PECL(PHP Extension Community Library)中,主要作用是搭建大容量的内存数据的临时存放区域,在分布式的时候作用体现的非常明显,否则不建议使用。

    《phper》电子杂志的第一期中,有黑夜路人写的关于Memcache的详细安装教程,本人在ubuntu上安装完运行的时候报错:
    /usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

    按照:《libeven、memcached、libmemcache安装》中的方法,使用:

    sudo ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2

    可以修正这个BUG

    通过新得立安装php的memcached模块,注销/etc/php5/conf.d/memcached.ini里面的“;”,重启apache,调用phpinfo()出现memcached的信息

    执行:

    /usr/local/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid

    memcached的服务正式启动

    Memcache::add — 添加一个值,如果已经存在,则返回false
    Memcache::addServer — 添加一个可供使用的服务器地址
    Memcache::close — 关闭一个Memcache对象
    Memcache::connect — 创建一个Memcache对象
    memcache_debug — 控制调试功能
    Memcache::decrement — 对保存的某个key中的值进行减法操作
    Memcache::delete — 删除一个key值
    Memcache::flush — 清除所有缓存的数据
    Memcache::get — 获取一个key值
    Memcache::getExtendedStats — 获取进程池中所有进程的运行系统统计
    Memcache::getServerStatus — 获取运行服务器的参数
    Memcache::getStats — 返回服务器的一些运行统计信息
    Memcache::getVersion — 返回运行的Memcache的版本信息
    Memcache::increment — 对保存的某个key中的值进行加法操作
    Memcache::pconnect — 创建一个Memcache的持久连接对象
    Memcache::replace — R对一个已有的key进行覆写操作
    Memcache::set — 添加一个值,如果已经存在,则覆写
    Memcache::setCompressThreshold — 对大于某一大小的数据进行压缩
    Memcache::setServerParams — 在运行时修改服务器的参数

    建议用面向对象的方式来测试这个库:

    <?php
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    $version = $memcache->getVersion();
    echo "Server's version: ".$version."<br/>\n";
    ?>

    Memcache::getVersion方法的作用是返回运行的Memcache的版本信息。

    Memcache::getStats方法的作用是返回服务器的一些运行统计信息。Memcache::getStats方法有三个参数,第一个参数表示要求返回的类型:reset, malloc, maps, cachedump, slabs, items, sizes;第二个参数和第三个参数是在第一个参数设置为“cachedump”时使用的。Memcache::getExtendedStats方法的作用是获取进程池中所有进程的运行系统统计。

    <?php
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    print_r($memcache->getStats());
    /**
     * Array
     * (
     *     [pid] => 8052
     *     [uptime] => 9205
     *     [time] => 1205898428
     *     [version] => 1.2.5
     *     [pointer_size] => 32
     *     [rusage_user] => 0.008000
     *     [rusage_system] => 0.000000
     *     [curr_items] => 1
     *     [total_items] => 17
     *     [bytes] => 57
     *     [curr_connections] => 2
     *     [total_connections] => 15
     *     [connection_structures] => 3
     *     [cmd_get] => 9
     *     [cmd_set] => 23
     *     [get_hits] => 5
     *     [get_misses] => 4
     *     [evictions] => 0
     *     [bytes_read] => 671
     *     [bytes_written] => 850
     *     [limit_maxbytes] => 10485760
     *     [threads] => 1
     * )
     */

    ?>

    Memcache::connect方法的作用是创建一个Memcache对象。Memcache::pconnect方法的作用是创建一个Memcache的持久连接对象。Memcache::close方法的作用是关闭一个Memcache对象。

    Memcache::set方法的作用是添加一个值,Memcache::set方法有四个参数,第一个参数是key,第二个参数是value,第三个参数可选,表示是否压缩保存,第四个参数可选,用来设置一个过期自动销毁的时间。Memcache::add方法的作用和Memcache::set方法类似,区别是如果Memcache::add方法的返回值为false,表示这个key已经存在,而Memcache::set方法则会直接覆写。Memcache::get方法的作用是获取一个key值,Memcache::get方法有一个参数,表示key。Memcache::replace方法的作用是对一个已有的key进行覆写操作,Memcache::replace方法有四个参数,作用和Memcache::set方法的相同。Memcache::delete方法的作用是删除一个key值,Memcache::delete方法有两个参数,第一个参数表示key,第二个参数可选,表示删除延迟的时间。

    <?php
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    $memcache->set( 'name', 'leo', 0, 30);
    if(!$memcache->add( 'name', 'susan', 0, 30))
    {
        
    echo 'susan is exist';
    };
    $memcache->replace( 'name', 'lion', 0, 300);
    echo $memcache->get( 'name');
    $memcache->delete( 'name', 5);
    ?>

    memcache_debug()函数的作用是控制调试功能,前提是php在编译的时候使用了–enable-debug选项,否则这个函数不会有作用。

    Memcache::addServer方法的作用是添加一个可供使用的服务器地址,Memcache::addServer方法有8个参数,除了第一个参数意外,其他都是可选的,第一个参数表示服务器的地址,第二个参数表示端口,第三个参数表示是否是一个持久连接,第四个参数表示这台服务器在所有服务器中所占的权重,第五个参数表示连接的持续时间,第六个参数表示连接重试的间隔时间,默认为15,设置为-1表示不进行重试,第七个参数用来控制服务器的在线状态,第8个参数允许设置一个回掉函数来处理错误信息。
    Memcache::setServerParams方法的作用是在运行时修改服务器的参数,Memcache::setServerParams方法有六个参数,Memcache::addServer方法少了第三和第四个参数。Memcache::getServerStatus方法的作用是获取运行服务器的参数,两个参数分别表示的地址和端口。

    <?php
    function _callback_memcache_failure($host, $port) {
        
    print "memcache '$host:$port' failed";
    }
    $memcache = new Memcache;
    $memcache->addServer('192.168.1.116', 11211);
    $memcache->setServerParams('192.168.1.116', 11211, 1, 15, true, '_callback_memcache_failure');
    echo $memcache->getServerStatus('192.168.1.116', 11211);
    ?>

    Memcache::flush方法的作用是清除所有缓存的数据,但是不会削去使用的内存空间。

    Memcache::increment方法的作用是对保存的某个key中的值进行加法操作,Memcache::decremen方法的作用是对保存的某个key中的值进行减法操作。

    <?php
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211);
    $memcache->set('test_item', 8);
    $memcache->increment('test_item', 4);
    echo $memcache->decrement('test_item', 7);
    // 显示 5
    ?>

    setCompressThreshold方法的作用是对大于某一大小的数据进行压缩。setCompressThreshold方法有两个参数,第一个参数表示处理数据大小的临界点,第二个参数表示压缩的比例,默认为0.2。

    <?php
    $memcache = new Memcache;
    $memcache->addServer('memcache_host', 11211);
    $memcache->setCompressThreshold(20000, 0.2);
    ?>
  • 例:有以下一个数组
    $post[0][0] = '梨子面馆 http://lizi.name ';
    $post[0][1] = '2007-09-29 09:10:53';
    $post[0][2] = 3;

    $post[15][0] = '小霸王孙策';
    $post[15][1] = '2006-09-29 09:10:53';
    $post[15][2] = 1;

    $post[1000][0] = '王者归来';
    $post[1000][1] = '2009-09-29 09:10:53';
    $post[1000][2] = 2;

    $post[150][0] = '成长的故事';
    $post[150][1] = '2007-12-29 09:10:53';
    $post[150][2] = 1;

    $post[250][0] = '获得及责任';
    $post[250][1] = '2007-11-29 09:10:53';
    $post[250][2] = 1;

    要求:第一按2的值排序,然后按1值的时间排序

    排序代码如下:
    foreach ($post as $key => $row) {
        $volume[$key]  = $row[2];
        $edition[$key] = $row[1];
    }

    // 将数据根据 volume 降序排列,根据 edition 升序排列
    // 把 $post 作为最后一个参数,以通用键排序
    array_multisort($volume, SORT_DESC, $edition, SORT_DESC, $post);
    print_r($post);

    结果

    Array
    (
        [0] => Array
            (
                [0] => 梨子面馆 http://lizi.name
                [1] => 2007-09-29 09:10:53
                [2] => 3
            )

        [1] => Array
            (
                [0] => 王者归来
                [1] => 2009-09-29 09:10:53
                [2] => 2
            )

        [2] => Array
            (
                [0] => 成长的故事
                [1] => 2007-12-29 09:10:53
                [2] => 1
            )

        [3] => Array
            (
                [0] => 获得及责任
                [1] => 2007-11-29 09:10:53
                [2] => 1
            )

        [4] => Array
            (
                [0] => 小霸王孙策
                [1] => 2006-09-29 09:10:53
                [2] => 1
            )

    )

    第二种方式,使用uasort函数

    uasort($post, create_function('$a,$b','$r1 = $a[2]-$b[2]; return $r1?$r1:strcmp($a[1],$b[1]);'));

  • 很多人使用JpGraph会出现乱码问题,但是网上有好多并无效果的解决方式,经过反复试验及在网上看了其它人的经验,总结出在2.3版下最终较完美解决方案
    1、把simsun.ttc字体拷到服务器/usr/X11R6/lib/X11/fonts/truetype下,当然这个目录可以自定义
    2、不需要更改$aFF === FF_SIMSUN或$aFF === FF_CHINESE ,直接用以下代码即可
    $title = "JpGraph中文测试";
    $title = iconv("UTF-8", "gb2312", $title);
    $graph->title->Set($title);
    就可以完美的解决中文问题了
    3、还有一个问题,就是右上方的中文显示问题,就是SetLegend函数,解决方式如下
    打开jpgraph.php文件,找到
    private $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;

    private $font_family=FF_SIMSUN,$font_style=FS_NORMAL,$font_size=8;
    替换就OK了^_^
  • 2007-12-24

    JpGraph - [技术文档]

    转自:笑笑驿站

    PHP绘图类库jpgraph,
    下载地址:http://www.aditus.nu/jpgraph/jpdownload.php
    开始以为他不支持中文,所以先在网上找了教程,教程写着要设置字库路径等。结果我下的那个版本一点都不用设置人家就写好了。如果是WIN系统就是根据环境变量找到系统的要目录。在加上“/fonts/”从而自动就找到字体了。
    当然如果是UNIX你可以把你WINDOWS/FONTS下的所有的TTF文件和一个simsun.ttc文件传入一个目录上。然后在jpgraph的src目录下找到jpgraph.php文件有一句
    DEFINE('TTF_DIR','/usr/X11R6/lib/X11/fonts/truetype/');
    改成你上传的那个字体目录就OK了。

    在你运行时可能会遇到说错误说你的PHP少FREETYPE2.支持
    你可以安装一下FREETYPE并编译进你的PHP。你只要在你的./configure选项加入一个 --with-freetype-dir=/usr/local/include/freetype2 我的freetype2是在这个下面的。你可以看看你安装在哪了。改成你的目录就可以了。


    只不过要有写代码时加入一个$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
    其中FF_SIMSUN就是中文,后面那个是字体的样试。



    背景图是可以自己指定的。
    这个其实没有什么难点中不过那个右上角的那些中文显示有点难度,因为那个类没有SetFont方法,也就是说那东西是不能显示中文的。所以我就改了基础类库
    修改了jpgraph.php文件里的。
    class Legend 类中有这样一句
       private $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;
    我把他改为
    private $font_family=FF_SIMSUN,$font_style=FS_NORMAL,$font_size=8;
    就OK了。

    代码如下:

    <?php

    include ("src/jpgraph.php");
    include ("src/jpgraph_bar.php");

    // Some data   所有的数据也就是我们想要的数据 
    $datay1=array(140,110,50,270,78);
    $datay2=array(35,90,190,190,56);
    $datay3=array(20,60,70,140,34);

    // Create the basic graph 设置图片属性
    $graph = new Graph(450,250,'auto');   //图片大小
    $graph->SetScale("textlin");    //图片类型
    $graph->img->SetMargin(40,90,30,40); //margin(填充)左、右、上、下

    // Adjust the position of the legend box
    $graph->legend->Pos(0.01,0.10);    //显示右上角的注释的位置 X,Y


    // Adjust the color for theshadow of the legend
    $graph->legend->SetShadow('darkgray@0.5');    //是否有阴影 类型@程度值 
    $graph->legend->SetFillColor('lightblue@0.3');   //填充颜色

    // Get localised version of the month names
    //var_dump($gDateLocale->GetShortMonth());
    $graph->xaxis->SetTickLabels(array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"));   // X 轴数据

    // Set a nice summer (in Stockholm) image  //如果有背影图 可以指定
    $graph->SetBackgroundImage('stship.jpg',BGIMG_COPY);

    // Set axis titles and fonts
    $graph->xaxis->title->Set('Year 2006');      //图片最右下角的文字。一般为单位
    $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);   //设置字体
    //设置X轴的说明。与图片的距离
    $graph->xaxis->title->SetMargin(8);

    $graph->xaxis->title->SetColor('white');

    $graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD);
    $graph->xaxis->SetColor('white');

    //$graph->yaxis->SetFont(FF_FONT1,FS_BOLD);     //设置 Y 轴
    $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
    $graph->yaxis->SetColor('white');

    //$graph->ygrid->Show(false);
    $graph->ygrid->SetColor('white@0.5');

    // Setup graph title 设置标题
    $graph->title->Set('会员域名注册量分析');
    // Some extra margin (from the top)
    $graph->title->SetMargin(3);
    $graph->title->SetFont(FF_SIMSUN,FS_BOLD,12);


    //创建三个柱图 其中数组$datay1-3是数据
    // Create the three var series we will combine
    $bplot1 = new BarPlot($datay1);
    $bplot2 = new BarPlot($datay2);
    $bplot3 = new BarPlot($datay3);

    //设置颜色并且有40%的透明度(alpha channel)
    // Setup the colors with 40% transparency (alpha channel)
    $bplot1->SetFillColor('orange@0.4');
    $bplot2->SetFillColor('brown@0.4');
    $bplot3->SetFillColor('darkgreen@0.4');

    //$bplot1->SetFont(FF_SIMSUN,FS_BOLD);

    // Setup legendsSetWidth //设置显示右上角的注释 文字显示
    $bplot1->SetLegend('北方区');
    $bplot2->SetLegend('华东区');
    $bplot3->SetLegend('华南区');

    //$bplot1->SetWidth(10);
    //$bplot2->SetWidth(10);
    //$bplot3->SetWidth(10);

    // Setup each bar with a shadow of 50% transparency
    $bplot1->SetShadow('black@0.4');
    $bplot2->SetShadow('black@0.4');
    $bplot3->SetShadow('black@0.4');

    //可以让每一个柱的真实数值显示是柱上
    $bplot1->value->Show();
    $bplot2->value->Show();
    $bplot3->value->Show();
    //
    $gbarplot = new GroupBarPlot(array($bplot1,$bplot2,$bplot3));
    //三个柱一个单元。设置这个单元的宽度
    $gbarplot->SetWidth(0.6);
    $graph->Add($gbarplot);
    //显示
    $graph->Stroke();
    ?>