<?php
/*正则取图片img标记中的任意属性*/
$word = '<p height="22" align="cenetr">111 22</p> <img" width=100% src="/upload/images/aaa.jpg" width="100" height="50"/><div style="float:left;">中国人</div>';
//取整个图片代码
preg_match('/<img(.[^<]*)src="?(.[^<"]*)"?(.[^<|/]*)/?>/is',$word,$matches);
echo $matches[0];//结果:<img" width=100% src="/upload/images/aaa.jpg" width="100">
$word = '<p height="22" align="cenetr">111 22</p> <img height="60"" width=100% src="/upload/images/aaa.jpg" width="100" style=><div style="float:left;">中国人</div>';
//取width
preg_match('/<img.+(width="?d*"?).+>/i',$word,$matches);
echo $matches[1];
//取height
preg_match('/<img.+(height="?d*"?).+>/i',$word,$matches);
echo $matches[1];
//取src
preg_match('/<img.+src="?(.+.(jpg|gif|bmp|bnp|png))"?.+>/i',$word,$matches);
echo $matches[1];
/*正则替换去掉或改变图片img标记中的任意属性***************************************************************/
$str = '<p height="22" align="cenetr">111 22</p> <img height="60"" width=100% src="/upload/images/aaa.jpg" width=100 style=><div style="float:left;">中国人</div><p height="22" align="cenetr">31313 224344</p> <img" width=100% src="/upload/images/bbb.jpg" height="60" width=100 style=><div style="float:left;">1212121</div>';
//改变src属性(此处将原来的src="/upload/images/bbb.jpg"改变为src="/upload/_thumbs/Images/bbb.jpg")
print preg_replace('/(<img.+src="?.+)(images/)(.+.(jpg|gif|bmp|bnp|png)"?.+>)/i',"${1}_thumbs/Images/${3}",$str);
/*改变src属性,
此处将原来的src="/upload/images/bbb.jpg"改变为src="/upload/_thumbs/Images/bbb.jpg",并舍弃宽和高
(比如你想在前台显示缩略图,但数据库教程中存储的是原图的路径。为什么要舍弃宽高??你缩略图啊!还是原图的宽高,会怎样???)
*/
print preg_replace('/(<img).+(src="?.+)images/(.+.(jpg|gif|bmp|bnp|png)"?).+>/i',"${1} ${2}_thumbs/Images/${3}>",$str);
预备知识
模板技术:
PHP模板引擎Smarty介绍
PHP配置使用Smarty技术
缓存技术:
比如经常不变的信息,但是还是需要改变的信息放在缓存中以加快显示速度,这是很有价值的。所谓的缓存,通俗的理解就是一些保存在服务器端的共用信息.它是于服务器同生死的,我们在保存缓存的时候可以指定下次更新的时间判断,比如每5分钟更新一次,可以记录上次更新的时间,和当前时间比较,如果大于 5 分钟-->读取数据库教程-->更新缓存,否则直接读取缓存数据,当然,缓存需要客户端用户激活的,只需一次.
ob_start()函数:打开输出缓冲区.
函数格式 void ob_start(void)
说明:当缓冲区激活时,所有来自PHP程序的非文件头信息均不会发送,而是保存在内部缓冲区。为了输出缓冲区的内容,可以使用ob_end_flush()或flush()输出缓冲区的内容。
Flush:刷新缓冲区的内容,输出
函数格式:flush()
说明:这个函数经常使用,效率很高。
ob_get_contents :返回内部缓冲区的内容
函数格式:string ob_get_contents(void)
说明:这个函数会返回当前缓冲区中的内容,如果输出缓冲区没有激活,则返回 FALSE.
ob_get_length:返回内部缓冲区的长度
函数格式:int ob_get_length(void)
说明:这个函数会返回当前缓冲区中的长度;和ob_get_contents一样,如果输出缓冲区没有激活,则返回 FALSE.
ob_end_clean:删除内部缓冲区的内容,并且关闭内部缓冲区
函数格式:void ob_end_clean(void)
说明:这个函数不会输出内部缓冲区的内容而是把它删除
ob_end_flush:发送内部缓冲区的内容到浏览器,并且关闭输出缓冲区
函数格式:void ob_end_flush(void)
说明:这个函数发送输出缓冲区的内容(如果有的话)
ob_implicit_flush:打开或关闭绝对刷新
函数格式:void ob_implicit_flush ([int flag])
说明:默认为关闭缓冲区,打开绝对输出后,每个脚本输出都直接发送到浏览器,不再需要调用 flush()
文件写入:
int fwrite ( resource handle, string string [, int length] )
fwrite() 把 string 的内容写入 文件指针 handle 处。 如果指定了 length,当写入了 length 个字节或者写完了 string 以后,写入就会停止,视乎先碰到哪种情况。
fwrite() 返回写入的字符数,出现错误时则返回 FALSE 。
相关参考官方网站: 文件参考
三、解决方案
思路:开启 ob_start缓冲,当已经调出数据的时候获取 ob_get_contents,然后生成静态页,ob_end_clean清除缓冲.ok,就这么来,来看一个例子(php+mysql教程的结合):
创建数据库:
CREATE TABLE `bihtml` (
`id` int(11) NOT NULL auto_increment,
`szdtitle` varchar(16) NOT NULL,
`szdcontent` text NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
获取当前的ID,并导入模板:
<?php
ob_start();
$id=_POST['id']
if(!isset($id)&&is_integer($id))
{
@$db=new mysqli('localhost','root','admin','bihtml');
$result=$db->fetch_one_array("select * from szd_bi where id='$id'");
if(!empty($result))
{
$tmp->assign(array(
"Szdtitle",htmlspecialchars($result['titles']),
"Szdcontent",$result['titles']));
}
$tpl->display('default_1.tpl');
$this_my_f= ob_get_contents(); //此处关键
ob_end_clean();
$filename = "$id.html";
if(tohtmlfile_cjjer($filename,$this_my_f))
echo "生成成功 $filename";
else
echo "生成识别";
}
}
//把生成文件的过程写出函数
function tohtmlfile_cjjer($file_cjjer_name,$file_cjjer_content)
{
if (is_file ($file_cjjer_name)){
@unlink ($file_cjjer_name);
}
$cjjer_handle = fopen ($file_cjjer_name,"w");
if (!is_writable ($file_cjjer_name)){
return false;
}
if (!fwrite ($cjjer_handle,$file_cjjer_content)){
return false;
}
fclose ($cjjer_handle); //关闭指针
return $file_cjjer_name;
}
?>
获取所有可用的模块 - get_loaded_extensions
该函数返回所有已经加载的(可用的)模块。
用法:
print_r(get_loaded_extensions());
2. 获取指定模块的可用函数 - get_extension_funcs
该函数返回指定模块所有可用的函数。传入的参数(模块名称)必须是小写
用法:
print_r(get_extension_funcs("gd"));
3. 获取所有已经定义的函数 - get_defined_functions
该函数返回所有已经定义的函数,包括内置函数和用户自定义函数。
用法:
function myrow($id, $data){
return "<tr><th>$id</th><td>$data</td></tr>\n";
}
$arr = get_defined_functions();
print_r($arr);
输出:
Array
(
[internal] => Array
(
[0] => zend_version
[1] => func_num_args
[2] => func_get_arg
[3] => func_get_args
[4] => strlen
[5] => strcmp
[6] => strncmp
...
[750] => bcscale
[751] => bccomp
)
[user] => Array
(
[0] => myrow
)
)
其中 $arr["internal"] 是内置函数, $arr["user"] 是用户自定义函数。
4. 检查指定函数是否存在 - function_exists
该函数返回指定函数是否已经定义。
用法:
if (function_exists('imap_open')) {
echo "IMAP functions are available.<br />\n";
} else {
echo "IMAP functions are not available.<br />\n";
}
<?php教程
// Ask for Input
fwrite(STDOUT, "Please Select Option(Default is Find) [1]Find [2]Replace Please Input Number: ");
// Get Input
$todo = trim(fgets(STDIN));
if(empty($todo)) $todo =1;
if($todo != 1 && $todo !=2){
echo "Selected Error! ";
exit;
}
if($todo==1){
fwrite(STDOUT,"Please Input Find Directory(Default is Current Directory):");
$dir = trim(fgets(STDIN));
if(empty($dir)){
$dir = getcwd();//当前目录
}else{
if(!is_dir($dir)){
echo "Directory Not Exist! ";
exit;
}
}
fwrite(STDOUT,"Please Input Content of the Find:");
$search = trim(fgets(STDIN));
echo "In Directory'".$dir."'Find'".$search."',Please Wait... ";
exec("find ".$dir." -exec grep --exclude='*.svn/*' -- '".$search."' {} +",$output);
foreach($output as $val){
echo "$val ";
}
}else{// write input back
fwrite(STDOUT, "Please Input Find Directory(Default is Current Directory):");
$dir = trim(fgets(STDIN));
if(empty($dir)){
$dir = getcwd();//当前目录
}else{
if(!is_dir($dir)){
echo "Directory Not Exist! ";
exit;
}
}
fwrite(STDOUT,"Please Input Prefix(Default is php):");
$ext = trim(fgets(STDIN));
if(empty($ext)) $ext = 'php';
fwrite(STDOUT,"Please Input Find Content:");
$search = trim(fgets(STDIN));
fwrite(STDOUT,"Please Input Replace Content:");
$replace = trim(fgets(STDIN));
echo "正在目录'".$dir."'查找后缀为'".$ext."'的文件,将内容'".$search."'替换为'".$replace."',请稍后... ";
exec("find ".$dir." -name '*.".$ext."' -exec sed --in-place 's/".$search."/$replace/g' {} ;");
echo "Replace Completed! ";
}
?>
/**************************************************************
02 *
03 * 使用特定function对数组中所有元素做处理
04 * @param string &$array 要处理的字符串
05 * @param string $function 要执行的函数
06 * @return boolean $apply_to_keys_also 是否也<SPAN class=t_tag onclick=tagshow(event) href="tag.php教程?name=%D3%A6%D3%C3">应用</SPAN>到key上
07 * @access public
08 *
09 *************************************************************/
10 function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
11 {
12 foreach ($array as $key => $value) {
13 if (is_array($value)) {
14 arrayRecursive($array[$key], $function, $apply_to_keys_also);
15 } else {
16 $array[$key] = $function($value);
17 }
18
19 if ($apply_to_keys_also && is_string($key)) {
20 $new_key = $function($key);
21 if ($new_key != $key) {
22 $array[$new_key] = $array[$key];
23 unset($array[$key]);
24 }
25 }
26 }
27 }
28
29 /**************************************************************
30 *
31 * 将数组转换为JSON字符串(兼容中文)
32 * @param array $array 要转换的数组
33 * @return string 转换得到的json字符串
34 * @access public
35 *
36 *************************************************************/
37 function JSON($array) {
38 arrayRecursive($array, 'urlencode', true);
39 $json = json_encode($array);
40 return urldecode($json);
41 }