今天整理商贸程序,其中得需要计算一个字符串中某个字符出现的次数,就上网搜,好不容易找到一个函数substr_count(),
<?php
$text = 'This is a test' ;
echo strlen ( $text ); // 14
echo substr_count ( $text , 'is' ); // 2
// the string is reduced to 's is a test', so it prints 1
echo substr_count ( $text , 'is' , 3 );
// the text is reduced to 's i', so it prints 0
echo substr_count ( $text , 'is' , 3 , 3 );
// generates a warning because 5+10 > 14
echo substr_count ( $text , 'is' , 5 , 10 );
// prints only 1, because it doesn't count overlapped subtrings
$text2 = 'gcdgcdgcd' ;
echo substr_count ( $text2 , 'gcdgcd' );
?>
<?php
$text = 'This is a test' ;
echo strlen ( $text ); // 14
echo substr_count ( $text , 'is' ); // 2
// the string is reduced to 's is a test', so it prints 1
echo substr_count ( $text , 'is' , 3 );
// the text is reduced to 's i', so it prints 0
echo substr_count ( $text , 'is' , 3 , 3 );
// generates a warning because 5+10 > 14
echo substr_count ( $text , 'is' , 5 , 10 );
// prints only 1, because it doesn't count overlapped subtrings
$text2 = 'gcdgcdgcd' ;
echo substr_count ( $text2 , 'gcdgcd' );
?>
看到很多朋友在各个地方发帖问php生成静态文章系统的方法,以前曾做过这样一个系统,遂谈些看法,以供各位参考。好了,我们先回顾一些基本的概念。
一,php脚本与动态页面。
php脚本是一种服务器端脚本程序,可通过嵌入等方法与html文件混合,也可以类,函数封装等形式,以模板的方式对用户请求进行处理。无论以何种方式,它的基本原理是这样的。由客户端提出请求,请求某一页面 -----> web服务器引入指定相应脚本进行处理 -----> 脚本被载入服务器 -----> 由服务器指定的php解析器对脚本进行解析形成html语言形式 ----> 将解析后的html语句以包的方式传回给浏览器。由此不难看出,在页面发送到浏览器后,php就不存在了,已被转化解析为html语句。客户请求为一动态文件,事实上并没有真正的文件存在在那里,是php解析而成相对应的页面,然后发送回浏览器。这种页面处理方式被称为“动态页面”。
二,静态页面。
一,php脚本与动态页面。
php脚本是一种服务器端脚本程序,可通过嵌入等方法与html文件混合,也可以类,函数封装等形式,以模板的方式对用户请求进行处理。无论以何种方式,它的基本原理是这样的。由客户端提出请求,请求某一页面 -----> web服务器引入指定相应脚本进行处理 -----> 脚本被载入服务器 -----> 由服务器指定的php解析器对脚本进行解析形成html语言形式 ----> 将解析后的html语句以包的方式传回给浏览器。由此不难看出,在页面发送到浏览器后,php就不存在了,已被转化解析为html语句。客户请求为一动态文件,事实上并没有真正的文件存在在那里,是php解析而成相对应的页面,然后发送回浏览器。这种页面处理方式被称为“动态页面”。
二,静态页面。
<?php
/**
------------------------
Function: php2html($in_Url, $out_htmlFile, $out_logFile)
------------------------
@ Description: 生成静态函数
@ Author 眼睛5462
@ QQ: 332458700 [E-mail:yanjing5462@my55.com.cn]
@ Copyright: Copyright (c) 2006 - 2011 MY55.com.cn
@ Create: 2006-08-01
@ Modify: 2006-10-27
@ 提示:这里要用到的路径为服务器绝对路径; 若给定的路径目录不存在则自动创建
=======================================================================================
@ Example:php2html("http://www.baidu.com", "/www/html/index.html", "/www/log/log.txt");
*/
// {{{ contents
function php2html($in_Url, $out_htmlFile, $out_logFile)
{
$htmlContent = file_get_contents($in_Url); //将文件读入 $htmlContent 变量
/**
* @检查要生成的文件是否存在
*/
if (is_file($out_htmlFile))
{
@unlink($out_htmlFile);//若文件已存在,则删除
}
/**
* @ 创建目录 网页部分
*/
$dir_array = explode("/", dirname($out_htmlFile));
chdir("/"); //改变目录到根
for($i=1;$i<count($dir_array);$i++)
{
if(is_dir($dir_array[$i]))
{
chdir($dir_array[$i]);
}
else
{
mkdir($dir_array[$i]);
chdir($dir_array[$i]);
}
}
/**
* @ 创建目录 日志部分
*/
$dir_array = explode("/", dirname($out_logFile));
chdir("/"); //改变目录到根
for($i=1;$i<count($dir_array);$i++)
{
if(is_dir($dir_array[$i]))
{
chdir($dir_array[$i]);
}
else
{
mkdir($dir_array[$i], 0777);
chdir($dir_array[$i]);
}
}
$handle = fopen($out_htmlFile, "w"); //打开文件指针,创建文件
$logHandle = fopen ($out_logFile, "a+"); //打开日志文件
/**
* @检查目录是否可写
*/
if (!is_writable($out_htmlFile))
{
echo "文件:".$out_htmlFile."不可写,请检查目录属性后重试";
exit();
}
if (!is_writable($out_logFile))
{
echo "文件:".$out_logFile."不可写,请检查目录属性后重试";
exit();
}
/**
* @写入文件
*/
if (!fwrite ($handle, $htmlContent))
{
$logMsg = "写入文件" . $out_htmlFile . "失败";
}
else
{
$logMsg = "创建文件" . $out_htmlFile . "成功";
}
/**
* @记录日志
*/
$logMsg .= "(".date("Y-m-d H:i:s") .")\r\n";
fwrite ($logHandle, $logMsg);
fclose($logHandle); //关闭日志指针
fclose ($handle); //关闭指针
}
// }}}
php2html("http://www.baidu.com", dirname(__FILE__)."/yanjing_html/index.html", dirname(__FILE__)."/yanjing_log/log.txt");
echo "成功";
?>
产生原因
主要有2个原因
1 xtmlhttp 返回的数据默认的字符编码是utf-8,如果前台页面是gb2312或者其它编码数据就会产生乱码
2 post方法提交数据默认的字符编码是utf-8,如果后台是gb2312或其他编码数据就会产生乱码
解决的办法就是在送出的流里面加一个HEADER,指明送出的是什么编码流,这样XMLHTTP就不会乱搞了。
PHP:header('Content-Type:text/html;charset=GB2312');
ASP:Response.Charset("GB2312")
JSP:response.setHeader("Charset","GB2312");
主要有2个原因
1 xtmlhttp 返回的数据默认的字符编码是utf-8,如果前台页面是gb2312或者其它编码数据就会产生乱码
2 post方法提交数据默认的字符编码是utf-8,如果后台是gb2312或其他编码数据就会产生乱码
解决的办法就是在送出的流里面加一个HEADER,指明送出的是什么编码流,这样XMLHTTP就不会乱搞了。
PHP:header('Content-Type:text/html;charset=GB2312');
ASP:Response.Charset("GB2312")
JSP:response.setHeader("Charset","GB2312");
1. 使用 mail() 函数
没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发。这个按照各个系统不同而定。使用参考手册。
2. 使用管道的形式
有网友曾经测试成功,使用本地的qmail来发送邮件。
没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发。这个按照各个系统不同而定。使用参考手册。
2. 使用管道的形式
有网友曾经测试成功,使用本地的qmail来发送邮件。
按钮代码:
<button onclick="copyToClipBoard()">复制本贴地址发送给好朋友</button>
调用函数
<script language="javascript">
function copyToClipBoard(){
var clipBoardContent="";
clipBoardContent+=document.URL
window.clipboardData.setData("Text",clipBoardContent);
alert("复制成功");
}
</script>
php之md5加密2006-08-19 13:03php5之取16位md5值
用php5实现
PHP代码:--------------------------------------------------------------------------------
<?php
/****
php5 get md5 value
****/
$value = md5("just a test", true);
echo $value;
?>
--------------------------------------------------------------------------------
用php4实现(不能直接取16位的,要么自己写个,要么截取)
PHP代码:--------------------------------------------------------------------------------
<?php
/****
php4 get md5 value
****/
$value = hex2bin(md5("just a test"));
echo $value;
/*
+--------------------------------------------------
| 函数名: hex2bin($data)
| 作用: 将16进转换为2进
| 参数: $data
|
|
| 返回值: 二进bit流
+--------------------------------------------------
*/
function hex2bin($data)
{
$len = strlen($data);
$newdata = '';
for($i=0;$i<$len;$i+=2)
{
$newdata .= pack("C",hexdec(substr($data,$i,2)));
}
return $newdata;
}
用php5实现
PHP代码:--------------------------------------------------------------------------------
<?php
/****
php5 get md5 value
****/
$value = md5("just a test", true);
echo $value;
?>
--------------------------------------------------------------------------------
用php4实现(不能直接取16位的,要么自己写个,要么截取)
PHP代码:--------------------------------------------------------------------------------
<?php
/****
php4 get md5 value
****/
$value = hex2bin(md5("just a test"));
echo $value;
/*
+--------------------------------------------------
| 函数名: hex2bin($data)
| 作用: 将16进转换为2进
| 参数: $data
|
|
| 返回值: 二进bit流
+--------------------------------------------------
*/
function hex2bin($data)
{
$len = strlen($data);
$newdata = '';
for($i=0;$i<$len;$i+=2)
{
$newdata .= pack("C",hexdec(substr($data,$i,2)));
}
return $newdata;
}
动态返回上一页代码:
Response.Redirect(Request.ServerVariables("HTTP_HOST"))
静态返回上一页代码:
javascript:history.back()
-----------------------------------------------------------------
<a href="javascript:history.back(-1)">返回上一页</a>
Response.Redirect(Request.ServerVariables("HTTP_HOST"))
静态返回上一页代码:
javascript:history.back()
-----------------------------------------------------------------
<a href="javascript:history.back(-1)">返回上一页</a>
方案1:
/// <summary>
/// 传入URL返回网页的html代码
/// </summary>
/// <param name="Url">URL</param>
/// <returns></returns>
public static string getUrltoHtml(string Url)
{
errorMsg = "";
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
// Get the response instance.
System.Net.WebResponse wResp =wReq.GetResponse();
// Read an HTTP-specific property
//if (wResp.GetType() ==HttpWebResponse)
//{
//DateTime updated =((System.Net.HttpWebResponse)wResp).LastModified;
//}
// Get the response stream.
System.IO.Stream respStream = wResp.GetResponseStream();
// Dim reader As StreamReader = New StreamReader(respStream)
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
return reader.ReadToEnd();
}
catch(System.Exception ex)
{
errorMsg = ex.Message ;
}
return "";
}
你可以用这个函数获取网页的客户端的html代码,然后保存到.html文件里就可以了。
/// <summary>
/// 传入URL返回网页的html代码
/// </summary>
/// <param name="Url">URL</param>
/// <returns></returns>
public static string getUrltoHtml(string Url)
{
errorMsg = "";
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
// Get the response instance.
System.Net.WebResponse wResp =wReq.GetResponse();
// Read an HTTP-specific property
//if (wResp.GetType() ==HttpWebResponse)
//{
//DateTime updated =((System.Net.HttpWebResponse)wResp).LastModified;
//}
// Get the response stream.
System.IO.Stream respStream = wResp.GetResponseStream();
// Dim reader As StreamReader = New StreamReader(respStream)
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
return reader.ReadToEnd();
}
catch(System.Exception ex)
{
errorMsg = ex.Message ;
}
return "";
}
你可以用这个函数获取网页的客户端的html代码,然后保存到.html文件里就可以了。