当前位置: 编程技术>php
本页文章导读:
▪使用PHP提取视频网站页面中的FLASH地址的代码
然后我用PHP实现了这个功能,我觉得用PHP来做这项工作简直是一种享受!使用其提供的强大的HTML页面处理函数和正则表达式,短短的几行代码就能搞定这个功能。 贴一下关键代码: 代码如.........
▪来自phpguru得Php Cache类源码
Cache的作用不用说大家都知道咯,这些天也面试了一些人,发现很多人框架用多了,基础都忘记了,你问一些事情,他总是说框架解决了,而根本不明白是怎么回事,所以也提醒大家应该注意平时基础.........
▪php cache类代码(php数据缓存类)
如果访问量大的话会给数据库造成很大的负担,所以对于变化不经常的内容要做好php 数据cache(缓存)是十分必要的,我做了一个简单的php“文件缓存”的类,希望对大家有所帮助。 思路是这.........
[1]使用PHP提取视频网站页面中的FLASH地址的代码
来源: 互联网 发布时间: 2013-11-30
然后我用PHP实现了这个功能,我觉得用PHP来做这项工作简直是一种享受!使用其提供的强大的HTML页面处理函数和正则表达式,短短的几行代码就能搞定这个功能。
贴一下关键代码:
<?php
//获取优酷页面中的flash地址
function get_flash_url(/blog_article/$url/index.html)
{
$lines = file($url);
foreach ($lines as $linenum=> $line) {
preg_match_all('|<input type="text" id="link2" value="([^<>]+)" />|',$line,$result);
$swfurl=$result[1][0];
if(!empty($swfurl))
return $swfurl;
}
}
?>
<?php
$url=$_SERVER["QUERY_STRING"];
$flashurl= get_flash_url($url);
echo ( $flashurl );
?>
比如这个文件我们存为 test.php,那么我们只需要运行 test.php?优酷视频的url 就可以解析出FLASH地址了。
思路很简单,就是先看看优酷视频网页的HTML代码里关键FLASH地址那段的特征。随便找个网页,比如我们可以看到这一段:
<div ><span >flash地址: </span> <input type="text" id="link2" value="http://player.youku.com/player.php/sid/XMTU1MzcxMzAw/v.swf" />
然后使用正则表达式来将其中的地址段匹配掉,就OK了。
贴一下关键代码:
代码如下:
<?php
//获取优酷页面中的flash地址
function get_flash_url(/blog_article/$url/index.html)
{
$lines = file($url);
foreach ($lines as $linenum=> $line) {
preg_match_all('|<input type="text" id="link2" value="([^<>]+)" />|',$line,$result);
$swfurl=$result[1][0];
if(!empty($swfurl))
return $swfurl;
}
}
?>
<?php
$url=$_SERVER["QUERY_STRING"];
$flashurl= get_flash_url($url);
echo ( $flashurl );
?>
比如这个文件我们存为 test.php,那么我们只需要运行 test.php?优酷视频的url 就可以解析出FLASH地址了。
思路很简单,就是先看看优酷视频网页的HTML代码里关键FLASH地址那段的特征。随便找个网页,比如我们可以看到这一段:
<div ><span >flash地址: </span> <input type="text" id="link2" value="http://player.youku.com/player.php/sid/XMTU1MzcxMzAw/v.swf" />
然后使用正则表达式来将其中的地址段匹配掉,就OK了。
[2]来自phpguru得Php Cache类源码
来源: 互联网 发布时间: 2013-11-30
Cache的作用不用说大家都知道咯,这些天也面试了一些人,发现很多人框架用多了,基础都忘记了,你问一些事情,他总是说框架解决了,而根本不明白是怎么回事,所以也提醒大家应该注意平时基础知识的积累,之后对一些问题才能游刃有余.
群里也有些朋友对基础知识很不屑,总说有能力就可以了,基础知识考不出来什么.对于这样的观点,我一直不苟同.
这个只是一点感概罢了. 下面看正题,介绍一个php的Cache类:
贴一下代码吧:下面也有下载地址,其实很简单,重要的是学习
<?php
/**
* o------------------------------------------------------------------------------o
* | This package is licensed under the Phpguru license. A quick summary is |
* | that for commercial use, there is a small one-time licensing fee to pay. For |
* | registered charities and educational institutes there is a reduced license |
* | fee available. You can read more at: |
* | |
* | http://www.phpguru.org/static/license.html |
* o------------------------------------------------------------------------------o
*/
/**
* Caching Libraries for PHP5
*
* Handles data and output caching. Defaults to /dev/shm
* (shared memory). All methods are static.
*
* Eg: (output caching)
*
* if (!OutputCache::Start('group', 'unique id', 600)) {
*
* // ... Output
*
* OutputCache::End();
* }
*
* Eg: (data caching)
*
* if (!$data = DataCache::Get('group', 'unique id')) {
*
* $data = time();
*
* DataCache::Put('group', 'unique id', 10, $data);
* }
*
* echo $data;
*/
class Cache
{
/**
* Whether caching is enabled
* @var bool
*/
public static $enabled = true;
/**
* Place to store the cache files
* @var string
*/
protected static $store = '/dev/shm/';
/**
* Prefix to use on cache files
* @var string
*/
protected static $prefix = 'cache_';
/**
* Stores data
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
* @param int $ttl How long to cache for (in seconds)
*/
protected static function write($group, $id, $ttl, $data)
{
$filename = self::getFilename($group, $id);
if ($fp = @fopen($filename, 'xb')) {
if (flock($fp, LOCK_EX)) {
fwrite($fp, $data);
}
fclose($fp);
// Set filemtime
touch($filename, time() + $ttl);
}
}
/**
* Reads data
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
*/
protected static function read($group, $id)
{
$filename = self::getFilename($group, $id);
return file_get_contents($filename);
}
/**
* Determines if an entry is cached
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
*/
protected static function isCached($group, $id)
{
$filename = self::getFilename($group, $id);
if (self::$enabled && file_exists($filename) && filemtime($filename) > time()) {
return true;
}
@unlink($filename);
return false;
}
/**
* Builds a filename/path from group, id and
* store.
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
*/
protected static function getFilename($group, $id)
{
$id = md5($id);
return self::$store . self::$prefix . "{$group}_{$id}";
}
/**
* Sets the filename prefix to use
*
* @param string $prefix Filename Prefix to use
*/
public static function setPrefix($prefix)
{
self::$prefix = $prefix;
}
/**
* Sets the store for cache files. Defaults to
* /dev/shm. Must have trailing slash.
*
* @param string $store The dir to store the cache data in
*/
public static function setStore($store)
{
self::$store = $store;
}
}
/**
* Output Cache extension of base caching class
*/
class OutputCache extends Cache
{
/**
* Group of currently being recorded data
* @var string
*/
private static $group;
/**
* ID of currently being recorded data
* @var string
*/
private static $id;
/**
* Ttl of currently being recorded data
* @var int
*/
private static $ttl;
/**
* Starts caching off. Returns true if cached, and dumps
* the output. False if not cached and start output buffering.
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
* @param int $ttl How long to cache for (in seconds)
* @return bool True if cached, false if not
*/
public static function Start($group, $id, $ttl)
{
if (self::isCached($group, $id)) {
echo self::read($group, $id);
return true;
} else {
ob_start();
self::$group = $group;
self::$id = $id;
self::$ttl = $ttl;
return false;
}
}
/**
* Ends caching. Writes data to disk.
*/
public static function End()
{
$data = ob_get_contents();
ob_end_flush();
self::write(self::$group, self::$id, self::$ttl, $data);
}
}
/**
* Data cache extension of base caching class
*/
class DataCache extends Cache
{
/**
* Retrieves data from the cache
*
* @param string $group Group this data belongs to
* @param string $id Unique ID of the data
* @return mixed Either the resulting data, or null
*/
public static function Get($group, $id)
{
if (self::isCached($group, $id)) {
return unserialize(self::read($group, $id));
}
return null;
}
/**
* Stores data in the cache
*
* @param string $group Group this data belongs to
* @param string $id Unique ID of the data
* @param int $ttl How long to cache for (in seconds)
* @param mixed $data The data to store
*/
public static function Put($group, $id, $ttl, $data)
{
self::write($group, $id, $ttl, serialize($data));
}
}
?>
使用方法:
$dir = !empty($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '.';
$dh = opendir($dir);
while ($filename = readdir($dh)) {
if ($filename == '.' OR $filename == '..') {
continue;
}
if (filemtime($dir . DIRECTORY_SEPARATOR . $filename) < time()) {
unlink($dir . DIRECTORY_SEPARATOR . $filename);
}
}
源码打包下载
群里也有些朋友对基础知识很不屑,总说有能力就可以了,基础知识考不出来什么.对于这样的观点,我一直不苟同.
这个只是一点感概罢了. 下面看正题,介绍一个php的Cache类:
贴一下代码吧:下面也有下载地址,其实很简单,重要的是学习
代码如下:
<?php
/**
* o------------------------------------------------------------------------------o
* | This package is licensed under the Phpguru license. A quick summary is |
* | that for commercial use, there is a small one-time licensing fee to pay. For |
* | registered charities and educational institutes there is a reduced license |
* | fee available. You can read more at: |
* | |
* | http://www.phpguru.org/static/license.html |
* o------------------------------------------------------------------------------o
*/
/**
* Caching Libraries for PHP5
*
* Handles data and output caching. Defaults to /dev/shm
* (shared memory). All methods are static.
*
* Eg: (output caching)
*
* if (!OutputCache::Start('group', 'unique id', 600)) {
*
* // ... Output
*
* OutputCache::End();
* }
*
* Eg: (data caching)
*
* if (!$data = DataCache::Get('group', 'unique id')) {
*
* $data = time();
*
* DataCache::Put('group', 'unique id', 10, $data);
* }
*
* echo $data;
*/
class Cache
{
/**
* Whether caching is enabled
* @var bool
*/
public static $enabled = true;
/**
* Place to store the cache files
* @var string
*/
protected static $store = '/dev/shm/';
/**
* Prefix to use on cache files
* @var string
*/
protected static $prefix = 'cache_';
/**
* Stores data
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
* @param int $ttl How long to cache for (in seconds)
*/
protected static function write($group, $id, $ttl, $data)
{
$filename = self::getFilename($group, $id);
if ($fp = @fopen($filename, 'xb')) {
if (flock($fp, LOCK_EX)) {
fwrite($fp, $data);
}
fclose($fp);
// Set filemtime
touch($filename, time() + $ttl);
}
}
/**
* Reads data
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
*/
protected static function read($group, $id)
{
$filename = self::getFilename($group, $id);
return file_get_contents($filename);
}
/**
* Determines if an entry is cached
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
*/
protected static function isCached($group, $id)
{
$filename = self::getFilename($group, $id);
if (self::$enabled && file_exists($filename) && filemtime($filename) > time()) {
return true;
}
@unlink($filename);
return false;
}
/**
* Builds a filename/path from group, id and
* store.
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
*/
protected static function getFilename($group, $id)
{
$id = md5($id);
return self::$store . self::$prefix . "{$group}_{$id}";
}
/**
* Sets the filename prefix to use
*
* @param string $prefix Filename Prefix to use
*/
public static function setPrefix($prefix)
{
self::$prefix = $prefix;
}
/**
* Sets the store for cache files. Defaults to
* /dev/shm. Must have trailing slash.
*
* @param string $store The dir to store the cache data in
*/
public static function setStore($store)
{
self::$store = $store;
}
}
/**
* Output Cache extension of base caching class
*/
class OutputCache extends Cache
{
/**
* Group of currently being recorded data
* @var string
*/
private static $group;
/**
* ID of currently being recorded data
* @var string
*/
private static $id;
/**
* Ttl of currently being recorded data
* @var int
*/
private static $ttl;
/**
* Starts caching off. Returns true if cached, and dumps
* the output. False if not cached and start output buffering.
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
* @param int $ttl How long to cache for (in seconds)
* @return bool True if cached, false if not
*/
public static function Start($group, $id, $ttl)
{
if (self::isCached($group, $id)) {
echo self::read($group, $id);
return true;
} else {
ob_start();
self::$group = $group;
self::$id = $id;
self::$ttl = $ttl;
return false;
}
}
/**
* Ends caching. Writes data to disk.
*/
public static function End()
{
$data = ob_get_contents();
ob_end_flush();
self::write(self::$group, self::$id, self::$ttl, $data);
}
}
/**
* Data cache extension of base caching class
*/
class DataCache extends Cache
{
/**
* Retrieves data from the cache
*
* @param string $group Group this data belongs to
* @param string $id Unique ID of the data
* @return mixed Either the resulting data, or null
*/
public static function Get($group, $id)
{
if (self::isCached($group, $id)) {
return unserialize(self::read($group, $id));
}
return null;
}
/**
* Stores data in the cache
*
* @param string $group Group this data belongs to
* @param string $id Unique ID of the data
* @param int $ttl How long to cache for (in seconds)
* @param mixed $data The data to store
*/
public static function Put($group, $id, $ttl, $data)
{
self::write($group, $id, $ttl, serialize($data));
}
}
?>
使用方法:
代码如下:
$dir = !empty($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '.';
$dh = opendir($dir);
while ($filename = readdir($dh)) {
if ($filename == '.' OR $filename == '..') {
continue;
}
if (filemtime($dir . DIRECTORY_SEPARATOR . $filename) < time()) {
unlink($dir . DIRECTORY_SEPARATOR . $filename);
}
}
源码打包下载
[3]php cache类代码(php数据缓存类)
来源: 互联网 发布时间: 2013-11-30
如果访问量大的话会给数据库造成很大的负担,所以对于变化不经常的内容要做好php 数据cache(缓存)是十分必要的,我做了一个简单的php“文件缓存”的类,希望对大家有所帮助。
思路是这样的:
对于一般的变量,把该变量变成php语言的格式,写到文件中,用时只要include这个文件就相当于加载了cache了;
对于array型的变量,把array转化为php语言定义array的字符串,写到文件中,用时也只要include就相当于加载了cache了;
缓存cache时间上的控制,通过获取缓存文件的创建时间和现在的时间进行对比,如果没有到更新时间,直接读取缓存,如果到了更新时间,查询数据库,返回数据,再更新缓存。(尚未实现)
下面是我的php-kcache类(php_kcache_class.php):
注:如果是缓存字符串,请把转义字符多写一个'\',即”\n”要写成”\\n”。
/*
//php-kcache class v_0.1
//Author: kangzj
//Email : kangzj@mail.bnu.edu.cn
//Blog : http://kangzj.net.ru
//作者不保证本程序没有bug,对于使用本程序
//而引起的任何问题不担负任何责任。
*/
class php_kcache {
//相对或者绝对目录,末尾不要加 '/'
var $cache_dir = './cache';
var $cache_extension = '.cache.php';
function set_cache($name, $value){
$pre = "< ?\n//Cache Created at: ".date('Y-m-d H:i:s')."\n";
if(!is_array($value)){
$value = $value;
$str = "\$$name = '$value';";
}else{
$str = "\$$name = " . $this->arrayeval($value) . ';';
}
$end = "\n?>";
echo $cache = $pre . $str . $end;
$cache_file = $this->cache_dir . '/' . $name . $this->cache_extension;
if($fp = @fopen($cache_file, 'wb')) {
fwrite($fp, $cache);
fclose($fp);
return true;
} else {
echo $cache_file;
exit('Can not write to cache files, please check cache directory ');
return false;
}
}
//将array变成字符串, 来自discuz!
function arrayeval($array, $level = 0) {
if(!is_array($array)) {
return "'".$array."'";
}
$space = '';
for($i = 0; $i < = $level; $i++) {
$space .= "\t";
}
$evaluate = "Array\n$space(\n";
$comma = $space;
if(is_array($array)) {
foreach($array as $key => $val) {
$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
$val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
if(is_array($val)) {
$evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
} else {
$evaluate .= "$comma$key => $val";
}
$comma = ",\n$space";
}
}
$evaluate .= "\n$space)";
return $evaluate;
}
}
最简单的调用方法:
include './php_kcache_class.php';
$pc = new php_kcache;
$a = array('a', 'b', 'c');
$pc->set_cache('a', addslashes($a));
复杂的调用方法(加上缓存时间控制的)——稍后补上….to be continued…
思路是这样的:
对于一般的变量,把该变量变成php语言的格式,写到文件中,用时只要include这个文件就相当于加载了cache了;
对于array型的变量,把array转化为php语言定义array的字符串,写到文件中,用时也只要include就相当于加载了cache了;
缓存cache时间上的控制,通过获取缓存文件的创建时间和现在的时间进行对比,如果没有到更新时间,直接读取缓存,如果到了更新时间,查询数据库,返回数据,再更新缓存。(尚未实现)
下面是我的php-kcache类(php_kcache_class.php):
注:如果是缓存字符串,请把转义字符多写一个'\',即”\n”要写成”\\n”。
代码如下:
/*
//php-kcache class v_0.1
//Author: kangzj
//Email : kangzj@mail.bnu.edu.cn
//Blog : http://kangzj.net.ru
//作者不保证本程序没有bug,对于使用本程序
//而引起的任何问题不担负任何责任。
*/
class php_kcache {
//相对或者绝对目录,末尾不要加 '/'
var $cache_dir = './cache';
var $cache_extension = '.cache.php';
function set_cache($name, $value){
$pre = "< ?\n//Cache Created at: ".date('Y-m-d H:i:s')."\n";
if(!is_array($value)){
$value = $value;
$str = "\$$name = '$value';";
}else{
$str = "\$$name = " . $this->arrayeval($value) . ';';
}
$end = "\n?>";
echo $cache = $pre . $str . $end;
$cache_file = $this->cache_dir . '/' . $name . $this->cache_extension;
if($fp = @fopen($cache_file, 'wb')) {
fwrite($fp, $cache);
fclose($fp);
return true;
} else {
echo $cache_file;
exit('Can not write to cache files, please check cache directory ');
return false;
}
}
//将array变成字符串, 来自discuz!
function arrayeval($array, $level = 0) {
if(!is_array($array)) {
return "'".$array."'";
}
$space = '';
for($i = 0; $i < = $level; $i++) {
$space .= "\t";
}
$evaluate = "Array\n$space(\n";
$comma = $space;
if(is_array($array)) {
foreach($array as $key => $val) {
$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
$val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
if(is_array($val)) {
$evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
} else {
$evaluate .= "$comma$key => $val";
}
$comma = ",\n$space";
}
}
$evaluate .= "\n$space)";
return $evaluate;
}
}
最简单的调用方法:
代码如下:
include './php_kcache_class.php';
$pc = new php_kcache;
$a = array('a', 'b', 'c');
$pc->set_cache('a', addslashes($a));
复杂的调用方法(加上缓存时间控制的)——稍后补上….to be continued…
最新技术文章: