//方法一
代码如下 |
复制代码 |
$str = '<a href="http://www.111cn.net" target="_blank" name="doc3_p"><img" width=100% src="" onload="setImgSize(this,170,170);"></a>';
preg_match_all ('|^<a href="(.*)".*|U',$str,$out, PREG_PATTERN_ORDER);
print_r($out); |
//方法二
代码如下 |
复制代码 |
preg_match_all('/href=['"]+(.*)['"]+/',$str,$matches);
var_dump($matches[1]); |
//方法三
代码如下 |
复制代码 |
//(?<=<as*)href=[^ '">]+ |
//方法四
代码如下 |
复制代码 |
preg_match("/<as*href="(.*?)"[^>]*>.*?</a>/is", $str, $aMatch);
print_r($aMatch[1]); |
//方法五
代码如下 |
复制代码 |
preg_match_all('/href=('|"|s)*([^'">s]+)/i',$str,$match);
print_r($match);
preg_match_all('/src=('|"|s)*([^'">s]+)/i',$str,$match);
print_r($match); |
/*
上面这正则得到url的地址都是用了php正则表达试来实现的,只是方法有一点不同了。
json_encode和json_decode的效率并没有比serialize和unserialize的效率高,在反序列化的时候性能相差两倍左右,PHP 5.3执行效率比PHP 5.2略有提升。
代码如下 |
复制代码 |
<?php教程
$target = array (
'name' => '全能头盔',
'quality' => 'Blue',
'ti_id' => 21302,
'is_bind' => 1,
'demand_conditions' =>
array (
'HeroLevel' => 1,
),
'quality_attr_sign' =>
array (
'HeroStrength' => 8,
'HeroAgility' => 8,
'HeroIntelligence' => 8,
),
);
$json = json_encode($target);
$seri = serialize($target);
echo "json : " . strlen($json) . " ";
echo "serialize : " . strlen($seri) . " ";
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
json_encode($target);
}
$etime = microtime(true);
echo "json_encode : " . ($etime - $stime) . " ";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
json_decode($json);
}
$etime = microtime(true);
echo "json_decode : " . ($etime - $stime) . " ";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
serialize($target);
}
$etime = microtime(true);
echo "serialize : " . ($etime - $stime) . " ";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
unserialize($seri);
}
$etime = microtime(true);
echo "unserialize : " . ($etime - $stime) . " ";
echo 'DONE.';
?>
|
/*
字符串A:"1,3,4,5,6,7,8,9,10,11,12,14,15,17,20,22,123,457",
例如我想知道这个字符串里面是否含有"2",这时候"12","20","22"等
*/
//一
代码如下 |
复制代码 |
if(in_array('2',explode(',',$str)) |
//二
代码如下 |
复制代码 |
$str= '1,3,4,5,6,7,8,9,10,11,12,14,15,17,20,22,123,457';
if(strpos(',' . $str . ',', ',2,') !== false)
{
//TODO
} |
//三
代码如下 |
复制代码 |
echo preg_match('/(?<=^|,)2(?=,|$)/','1,3,4,5,6,7,8,9,10,11,12,14,15,17,20,22,123,457'); |
//方法一
代码如下 |
复制代码 |
echo preg_replace('#[x{4e00}-x{9fa5}]#ue','chinese_unicode("\0")',"您好,中国");//保证"您好,中国"是utf-8。
function chinese_unicode($c) {
return "u".dechex(((ord($c[0]) & 0x1f) << 12) + (ord($c[1]) & 0x3f << 6) + (ord($c[2]) & 0x3f)); |
}
//方法二
代码如下 |
复制代码 |
foreach(unpack(
'n*',
mb_convert_encoding('你好', 'unicode', 'gbk')
) as $i) {
echo 'u',dechex($i); |
}
本文章为你提供这款php文件下载代码是一款利用header把文件代码发送到客户端的浏览器进行下载哦。
代码如下 |
复制代码 |
<?
function download($file_dir,$file_name)
//参数说明:
//file_dir:文件所在目录
//file_name:文件名
{
$file_dir = chop($file_dir);//去掉路径中多余的空格
//得出要下载的文件的路径
if($file_dir != '')
{
$file_path = $file_dir;
if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
$file_path .= '/';
$file_path .= $file_name;
}
else
$file_path = $file_name;
//判断要下载的文件是否存在www.111cn.net
if(!file_exists($file_path))
{
echo '对不起,你要下载的文件不存在。';
return false;
}
$file_size = filesize($file_path);
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");//111cn.net
header("Accept-Length: $file_size");
header("Content-Disposition: attachment; filename=".$file_name);
$fp = fopen($file_path,"r");
$buffer_size = 1024;
$cur_pos = 0;
while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
{
$buffer = fread($fp,$buffer_size);
echo $buffer;
$cur_pos += $buffer_size;
}
$buffer = fread($fp,$file_size-$cur_pos);
echo $buffer;
fclose($fp);
return true;
}
?>
|
标签:[!--infotagslink--]