当前位置: 编程技术>php
本页文章导读:
▪php读取html并截取字符串的小例子 本节内容:
php读取html并截取字符串
例子:
代码示例:
<?php
/**
* 读取html并截取字符串
* by unun
*/
$title='';
$hello='!';
$file=file_get_contents('http://www.baidu.com');
$file=iconv("gbk//IGNORE","utf-8",.........
▪php判断str或xml格式的小函数 例子:
代码示例:
<?php
/**
* 解析XML格式的字符串
* @edit: www.
* @param string $str
* @return 解析正确就返回解析结果,否则返回false,说明字符串不是XML格式
.........
▪php操作配置文件的实例 本节内容:
php操作配置文件的例子
1,php管理配置文件的类
代码示例:
<?php
define("__CR__", "\r\n"); //回车换行符
class fileConfigOp
{
/*配置文件*/
var $file = '';
/**
* 构.........
[1]php读取html并截取字符串的小例子
来源: 互联网 发布时间: 2013-12-24
本节内容:
php读取html并截取字符串
例子:
代码示例:
<?php
/**
* 读取html并截取字符串
* by unun
*/
$title='';
$hello='!';
$file=file_get_contents('http://www.baidu.com');
$file=iconv("gbk//IGNORE","utf-8",$file);
//echo $file;
echo strpos($file,'<title>');
echo substr($file,strpos($file,'<title>')+7,strpos($file,'</title>'-strpos($file,'<title>')+7));
$file=str_replace()(array('百度一下','{hello}'),array($title,$hello), $file);
?>
/**
* 读取html并截取字符串
* by unun
*/
$title='';
$hello='!';
$file=file_get_contents('http://www.baidu.com');
$file=iconv("gbk//IGNORE","utf-8",$file);
//echo $file;
echo strpos($file,'<title>');
echo substr($file,strpos($file,'<title>')+7,strpos($file,'</title>'-strpos($file,'<title>')+7));
$file=str_replace()(array('百度一下','{hello}'),array($title,$hello), $file);
?>
附,一个小偷程序代码。
代码示例:
<?php
/**
* 网站小偷程序
* by unun
*/
function _url(/blog_article/$Date/index.html){
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, "$Date");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
$url="http://www.";
$contents.=_url(/blog_article/$url/index.html);
echo $contents; //输出内容
?>
/**
* 网站小偷程序
* by unun
*/
function _url(/blog_article/$Date/index.html){
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, "$Date");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
$url="http://www.";
$contents.=_url(/blog_article/$url/index.html);
echo $contents; //输出内容
?>
您可能感兴趣的文章:
PHP中文字符串截取的四个函数
php 截取指定长度字符串的代码分享
支持中文的php字符串截取函数实例
php字符串截取(substr的应用与扩展)
四个php中文字符串截取函数
php计算中文字符串长度、截取中文字符串的函数
字符串截取函数(汉字一个算两个字符,英文算一个字符)
PHP中文字符串截取函数
[2]php判断str或xml格式的小函数
来源: 互联网 发布时间: 2013-12-24
例子:
代码示例:
<?php
/**
* 解析XML格式的字符串
* @edit: www.
* @param string $str
* @return 解析正确就返回解析结果,否则返回false,说明字符串不是XML格式
*/
function xml_parser($str){
$xml_parser = xml_parser_create();
if(!xml_parse($xml_parser,$str,true)){
xml_parser_free($xml_parser);
return false;
}else {
return (json_decode()(json_encode(simplexml_load_string($str)),true));
}
}
/**
* 解析XML格式的字符串
* @edit: www.
* @param string $str
* @return 解析正确就返回解析结果,否则返回false,说明字符串不是XML格式
*/
function xml_parser($str){
$xml_parser = xml_parser_create();
if(!xml_parse($xml_parser,$str,true)){
xml_parser_free($xml_parser);
return false;
}else {
return (json_decode()(json_encode(simplexml_load_string($str)),true));
}
}
[3]php操作配置文件的实例
来源: 互联网 发布时间: 2013-12-24
本节内容:
php操作配置文件的例子
1,php管理配置文件的类
代码示例:
<?php
define("__CR__", "\r\n"); //回车换行符
class fileConfigOp
{
/*配置文件*/
var $file = '';
/**
* 构造函数
*
* @param string $file 配置文件
* @return null
*/
function __construct($file = '')
{
if (empty($file)) {
die("Not specify config file!");
}
if (!file_exists($file)) {
die("File '{$this->file}' does not exists!");
}
if (!is_readable($file)) {
die("File '{$this->file}' is not readable!");
}
$this->file =& $file;
}
/**
* 获取配置文件的变量
*
* @param string $varName 变量名称
* @return string|array 如果指定变量名称,则只返回该变量对应的值,否则返回所有变量所对应的一维数组
*/
function get($varName = '')
{
@$array = file($this->file);
if (empty($array)) {
return '';
}
foreach ($array AS $line)
{
$line=trim($line);
if (empty($line)||$line==""||substr($line,0,1)=='#'){
continue;
}
$temp = explode()("=",$line);
$var_name = trim($temp[0]);
$var_value = trim($temp[1]);
if (empty($var_name)) {
continue;
}
if(substr($var_value,-1)==';'){
$var_value=substr($var_value,0,strlen($var_value)-1);
}
if(substr($var_value,0,1)=="'"&&substr($var_value,-1)=="'"){
$var_value=substr($var_value,1,strlen($var_value)-2);
}
if(substr($var_value,0,1)=='"'&&substr($var_value,-1)=='"'){
$var_value=substr($var_value,1,strlen($var_value)-2);
}
if ($varName != '' && $var_name == $varName) {
return $var_value;
}
$vars[$var_name] = $var_value;
}
return $vars;
}
/**
* 存储变量到配置文件中
* 不删除配置文件中原有的变量,
* 如果需要保存的变量在配置文件中已经存在,则覆盖该变量的值,否则追加存储
*
* @param array $array 需要保存的数组变量,一维数组
* @return bollen
*/
function save($array = '')
{
if ((!is_array($array)) || empty($array)) {
return false;
}
if (!is_writable($this->file)) {
die("File '{$this->file}' is not writeable, please CHMOD it to 777!");
}
$vars = $this->get();
$vars = !empty($vars) ? array_merge($vars, $array) : $array;
$str='';
foreach ($vars AS $key => $value)
{
$str .= trim($key) . ' = "' . trim($value).'"' . __CR__;
}
//dump($str);
if (!empty($str)) {
@$fd =&fopen($this->file,"w+");
@fputs($fd,$str);
@fclose($fd);
}
return true;
}
/**
* 删除一个配置
* @param unknown_type $varName
*/
function del($varName = ''){
if(empty($varName)){
return false;
}
if (!is_writable($this->file)) {
die("File '{$this->file}' is not writeable, please CHMOD it to 777!");
}
$vars = $this->get();
$str='';
$flag=0;
foreach ($vars as $key=>$value){
if($key!=$varName){
$str .= trim($key) . ' = "' . trim($value).'"' . __CR__;
}else {
$flag=1;
}
}
if (!empty($str)&&$flag==1) {
@$fd =&fopen($this->file,"w+");
@fputs($fd,$str);
@fclose($fd);
}
return true;
}
}
define("__CR__", "\r\n"); //回车换行符
class fileConfigOp
{
/*配置文件*/
var $file = '';
/**
* 构造函数
*
* @param string $file 配置文件
* @return null
*/
function __construct($file = '')
{
if (empty($file)) {
die("Not specify config file!");
}
if (!file_exists($file)) {
die("File '{$this->file}' does not exists!");
}
if (!is_readable($file)) {
die("File '{$this->file}' is not readable!");
}
$this->file =& $file;
}
/**
* 获取配置文件的变量
*
* @param string $varName 变量名称
* @return string|array 如果指定变量名称,则只返回该变量对应的值,否则返回所有变量所对应的一维数组
*/
function get($varName = '')
{
@$array = file($this->file);
if (empty($array)) {
return '';
}
foreach ($array AS $line)
{
$line=trim($line);
if (empty($line)||$line==""||substr($line,0,1)=='#'){
continue;
}
$temp = explode()("=",$line);
$var_name = trim($temp[0]);
$var_value = trim($temp[1]);
if (empty($var_name)) {
continue;
}
if(substr($var_value,-1)==';'){
$var_value=substr($var_value,0,strlen($var_value)-1);
}
if(substr($var_value,0,1)=="'"&&substr($var_value,-1)=="'"){
$var_value=substr($var_value,1,strlen($var_value)-2);
}
if(substr($var_value,0,1)=='"'&&substr($var_value,-1)=='"'){
$var_value=substr($var_value,1,strlen($var_value)-2);
}
if ($varName != '' && $var_name == $varName) {
return $var_value;
}
$vars[$var_name] = $var_value;
}
return $vars;
}
/**
* 存储变量到配置文件中
* 不删除配置文件中原有的变量,
* 如果需要保存的变量在配置文件中已经存在,则覆盖该变量的值,否则追加存储
*
* @param array $array 需要保存的数组变量,一维数组
* @return bollen
*/
function save($array = '')
{
if ((!is_array($array)) || empty($array)) {
return false;
}
if (!is_writable($this->file)) {
die("File '{$this->file}' is not writeable, please CHMOD it to 777!");
}
$vars = $this->get();
$vars = !empty($vars) ? array_merge($vars, $array) : $array;
$str='';
foreach ($vars AS $key => $value)
{
$str .= trim($key) . ' = "' . trim($value).'"' . __CR__;
}
//dump($str);
if (!empty($str)) {
@$fd =&fopen($this->file,"w+");
@fputs($fd,$str);
@fclose($fd);
}
return true;
}
/**
* 删除一个配置
* @param unknown_type $varName
*/
function del($varName = ''){
if(empty($varName)){
return false;
}
if (!is_writable($this->file)) {
die("File '{$this->file}' is not writeable, please CHMOD it to 777!");
}
$vars = $this->get();
$str='';
$flag=0;
foreach ($vars as $key=>$value){
if($key!=$varName){
$str .= trim($key) . ' = "' . trim($value).'"' . __CR__;
}else {
$flag=1;
}
}
if (!empty($str)&&$flag==1) {
@$fd =&fopen($this->file,"w+");
@fputs($fd,$str);
@fclose($fd);
}
return true;
}
}
2,调用实例:
代码示例:
<?php
$ini = new fileConfigOp("venocap.conf"); //对象实例化
$t1=$ini->get(); //获取所有变量名和值,返回:一维数组
$t2=$ini->get("nic"); //获取某个变量的值,返回:字符串
dump($t1); // www.
dump($t2);
$array=array();
$array['nic1']='eth0';
$array['nic']='eth1';
dump($ini->save($array));
dump($ini->get());
dump($ini->del('nic1'));
dump($ini->get());
$ini = new fileConfigOp("venocap.conf"); //对象实例化
$t1=$ini->get(); //获取所有变量名和值,返回:一维数组
$t2=$ini->get("nic"); //获取某个变量的值,返回:字符串
dump($t1); // www.
dump($t2);
$array=array();
$array['nic1']='eth0';
$array['nic']='eth1';
dump($ini->save($array));
dump($ini->get());
dump($ini->del('nic1'));
dump($ini->get());
3,配置文件格式:
# network interface for capturing
nic = "eth0";
# redirection URL for HTTP request in IPv4
url = "http://www.";
# redirection URL for HTTP request in IPv6
url6 = "http://ipv6.";
# libpcap filter, see tcpdump manual for reference
# The following filter means capturing tcp target port 80,
# excluding ip address 2001:dc7:eee4:1::98 (i.e. ipv6.) and 221.122.51.98 (www.)
filter = "dst port 80 and ! ip6 host 2001:dc7:eee4:1::98 and ! ip host 221.122.51.98";
# HTTP request containing the following User-Agent will be bypassed
agent = "veno";
# controlling log message level
# logmask = 7; # trace message
# logmask = 15; # highest verbosity
nic = "eth0";
# redirection URL for HTTP request in IPv4
url = "http://www.";
# redirection URL for HTTP request in IPv6
url6 = "http://ipv6.";
# libpcap filter, see tcpdump manual for reference
# The following filter means capturing tcp target port 80,
# excluding ip address 2001:dc7:eee4:1::98 (i.e. ipv6.) and 221.122.51.98 (www.)
filter = "dst port 80 and ! ip6 host 2001:dc7:eee4:1::98 and ! ip host 221.122.51.98";
# HTTP request containing the following User-Agent will be bypassed
agent = "veno";
# controlling log message level
# logmask = 7; # trace message
# logmask = 15; # highest verbosity
最新技术文章: