随手笔记

一、用来遍历二维数组

for ($i = 0; $i < count($hno); $i++) {
  foreach($hno[$i] as $k =>$v){
    echo $v."<br/>";
  }
}

二、从网上下载图片

$url='http://exp.cdn-hotels.com/hotels/1000000/530000/527300/527252/527252_50_z.jpg';

方法一、(最简单)
$filename = dirname(__FILE__)."/imgs/".date("Ymdhis").".jpg";
copy($url,$filename);

方法二、
$fileName=dirname(__FILE__)."/imgs/".date("Ymdhis").".jpg";
getImage($url , $fileName );

function getImage($url , $fileName )
{
$ch = curl_init();
$fp = fopen($fileName, 'wb');

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

curl_exec($ch);
curl_close($ch);
fclose($fp);
}

方法三、
$curl = curl_init($url);
$filename = dirname(__FILE__)."/imgs/".date("Ymdhis").".jpg";
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$imageData = curl_exec($curl);
curl_close($curl);
$tp = @fopen($filename, 'ab');
fwrite($tp, $imageData);
fclose($tp);

三、创建文件夹

$path_small="C:/myWorkspace/hotel_img/small/$no/";
if (!file_exists($path_big)) {
mkdir($path_big);
}

四、以下代码可以遍历指定路径下的文件夹,找出数据库中的记录数与文件夹的差集
<?php
include_once 'db_conn.php';
$dir ="C:myWorkspacehotel_imgsmall";
$files1 = scandir($dir);

//echo count($files1);
//print_r($files1);exit();

$sql="SELECT no FROM 0hotelone_img GROUP BY no";
$res=mysql_query($sql);
$temp=array();
while($row=mysql_fetch_assoc($res)){
$temp[]=$row;
}
//把二维数组转成一维数组
$list=array();
foreach ($temp as $no){
$list[]=$no[no];
}
//echo count($temp);
//print_r($list);exit();
//array_diff()返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的值
print_r(array_diff ( $list , $files1 ));
?>

五、Unicode编码转换成汉字
$str='&#26223;&#35266;&#23458;&#25151;&#65288;&#19968;&#24352;&#29305;&#22823;&#24202;&#65289;';
echo unescape($str);exit();
function unescape($str) {
$str = rawurldecode($str);
preg_match_all("/(?:%u.{4})|&#x.{4};|&#d+;|.+/U",$str,$r);
$ar = $r[0];
//print_r($ar);
foreach($ar as $k=>$v) {
if(substr($v,0,2) == "%u"){
$ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,-4)));
}
elseif(substr($v,0,3) == "&#x"){
$ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,3,-1)));
}
elseif(substr($v,0,2) == "&#") {
$ar[$k] = iconv("UCS-2BE","UTF-8",pack("n",substr($v,2,-1)));
}
}
return join("",$ar);
}

六、
复制下边的代码,粘贴到你网页中的任意位置,浮层将出现在你的网页中。
如果你的网站使用了模板,你也可以粘帖代码到模板中
<!-- Wenjuan Layer Begin -->
<div id="idy_floatdiv" style="position:fixed;right:0;top:250px;">
<a href="http://www.wenjuan.com/s/YramQ3/" target="blank">

<img src="http://www.wenjuan.com/static/images/sharepic/share_pA.png" />

</a>
</div>
<!-- Wenjuan Layer End -->

回到顶部的锚链接
<div id="idy_floatdiv" style="position:fixed;right:10px;top:500px;">
<a href="#">回<br/>到<br/>顶<br/>部</a>
</div>

七、continue  的用法

for($i=0;$i<21;$i++){
if(($i%2)== 0){
echo $i.' ';
continue;//表示循环继续执行,continue 后面的代码将不会执行
echo '/';
}
}

原文地址:https://www.cnblogs.com/linjinzhuang/p/4691648.html