這樣做的原因是,,為了避免不耐煩,分成幾章來解釋, 。
三種方式:
1.動態(tài)網(wǎng)站 , 靜態(tài)時直接使用file_ get_ contents(' http://ww. ABC.com/about');獲取網(wǎng)頁html源碼,,然后使用file_ put_ contents(' e: \ web/html/about. HTML',HTML源碼)創(chuàng)建靜態(tài)文件;
2.做好靜態(tài)模板,.把標(biāo)識符,添加到模塊內(nèi)容顯示的地方 。例如,公司產(chǎn)品列表,# #產(chǎn)品列表# # , , 在生成靜態(tài)時讀取模板內(nèi)容 , str_ replace(# Productlist# , $ prolist,$ mubanner);$prolist是我們在數(shù)據(jù)庫中讀到的產(chǎn)品列表;
3.,比較麻煩的是把,的說法寫在模板里,然后分析 , ,的好處 , 可以減少后臺程序的改動;
我們主要說第三種方法主要使用,,的preg_ match_ all,,, , 的preg_ match效率不高,對于loop,作為企業(yè)站生成靜態(tài)頁面,使用多次應(yīng)該就夠了 。
preg_match($preg, $str, $match);截取第一個
preg_match_all($preg, $str, $match);截取所有的
參數(shù)描述:
1.$preg:常規(guī),;搜索的模式
2.$str:搜索到的字符串;
3.$match:返回,數(shù)組;
例1:截取include/header和include/footer 。
$ str=“{包含/頁眉} {包含/頁腳}”;
代碼如下:
$preg='/{include( 。*)}/';preg_match_all($preg,$str,$ match);print_ r($ match[1]);
描述:
1.常規(guī)開局,結(jié)束于2/內(nèi)線;
2.開始標(biāo)志“{include”和 , 結(jié)束標(biāo)志“}”;
3.*任何,字符都不包括換行符;
4.(.*)括號和無返回值的區(qū)別是:沒有返回帶有開始和結(jié)束標(biāo)志的數(shù)組元素, , 只有$ match[0],沒有1;之后,,返回兩個數(shù)組元素$match[0]不變,,$ match[1]沒有開始和結(jié)束的符號;
輸出:
數(shù)組([0]=包含/頁眉[1]=包含/頁腳)
在獲得包含的標(biāo)題底部的文件名,后,我們可以直接讀取相應(yīng)的模板 , 并替換它 。
具體操作:
for($ I=0;$ I=count($ match[1])-1;$i){//讀取對應(yīng)的模板內(nèi)容$ thtml=file_ get_ contents($ match[1][$ I]);$str=str_replace($match[0][$i],$thtml,$ str);}
例2:preg_match截取第一個include標(biāo)簽中的內(nèi)容(字符串“111”) 。
$ str=' { include} 111 {/include} 222 { include} 333 {/include} ';$preg='/{include}( 。*?){ \/include}/s';preg_match($preg,$str,$ match);print_ r($ match);
描述:
1.添加修改器s,來強(qiáng)制一行;
2.*?比例1增加了一個問號 。如果沒有添加,結(jié)束標(biāo)記 , ,將使用字符串的最后一次出現(xiàn);
3.$match[0]包含開始和結(jié)束標(biāo)志;,$ match[1]不存在;
例二輸出結(jié)果:
Array([0] => {include}111{/include}[1] =>111)
例3:
$str='阿斯頓開好會加速的和卡{cmlist "t":"web_pic","f":"pic_path","c":"pic_cat=3","o":"sort desc,id desc","l":"10","name":"a"}這是要截取的字符串1{/cmlist}大豪科技阿斯頓開好會加速的和卡{cmlist"t":"web_page","f":"page_content","c":"id=80","o":"","name":"a"}這是要截取的字符串2{/cmlist}大豪科技';
截取cmlist 中的參數(shù),還有 這是要截取的字符串1 , 這是要截取的字符串2
$preg="/{cmlist(.*?)}(.*?){\/cmlist}/is";preg_match_all($preg, $str, $match);print_r($match[1]);
說明:
1. 這里用到了修飾符i和 s , 因為給出來的$str是換行的加上修飾符s可以把字符串當(dāng)成一行來匹配,i不區(qū)分大小寫,例如:CMLIST 和cmlist是一個效果;
2. 結(jié)束標(biāo)志包含了“/”需要轉(zhuǎn)義前邊加上“\”;
3. $match[1]是截取到的參數(shù) , $match[2]是我們截取到的字符串;
$match[1]輸出結(jié)果:
Array([0] =>"t":"web_pic","f":"pic_path","c":"pic_cat=3","o":"sort desc,id desc","l":"10","name":"a"[1] =>"t":"web_page","f":"page_content","c":"id=80","o":"","name":"a")
$match[2]輸出結(jié)果:
Array([0] => 這是要截取的字符串1[1] => 這是要截取的字符串2)
查看$match[1]輸出結(jié)果,我們會發(fā)現(xiàn)和json格式很像只是2邊差了花括號{} , 我們手動補(bǔ)充上 。
【怎么自己免費(fèi)創(chuàng)建網(wǎng)站,如何在網(wǎng)上創(chuàng)建網(wǎng)站】具體操作:
for($i=0;$i<=count($match[1])-1;$i){ echo="" json_decode("{".$match[1][$i]."}")-="">t;exit;}輸出結(jié)果:web_pic
上邊是轉(zhuǎn)成了對象,也可以轉(zhuǎn)成數(shù)組 。
for($i=0;$i<=count($match[1])-1;$i){ print_r(="" json_decode("{".$match[1][$i]."}",true));="">=count($match[1])-1;$i){>
輸出結(jié)果:
Array([t] => web_pic[f] => pic_path[c] => pic_cat=3[o] => sort desc,id desc[l] => 10[name] => a)
可以和tp的查詢語句一一對應(yīng)
$list=Db::name(t)->field(f)->where(c)->order(o)->limit(l)->select();
foreach($list as $$name){}
例4:
$str='{cmlist"t":"web_cat","f":"cat_name,cat_html_path,cat_html_name","c":"parentid=0 and projectid=81","o":"sort desc,id desc","l":"","name":"a"}{fa cat_name|}{/cmlist}';
1. 要截取cat_html_path、cat_html_name、cat_name;
2. 豎線分隔,definefuc為自定義函數(shù)名稱 , 左側(cè)為參數(shù)變量,參數(shù)為常量放到右邊(conststr對應(yīng),funcname對應(yīng)函數(shù)名稱);
3. fa中的a是動態(tài)地對應(yīng)了cmlist中的name;
4. 如果name:a , 對應(yīng)fa,如果name:aa , 對應(yīng)的是faa;
5. 按照例2中的方法,在for循環(huán)中再次截取 以{fa開頭,以}結(jié)尾就得到了我們想要的字符串,這些字符串對應(yīng)了表的字段 。
6. $match[2]是我們要重新截取的原字符串;
操作如下:
for($i=0;$i<=count($match[1])-1;$i){ $name="json_decode(" {".$match[1][$i]."}")-"="">name;$fielpreg="/{f".$name."(.*?)}/";preg_match_all($fielpreg, $match[2][$i], $fiearr);//$fiearr就是我們得到的字段的數(shù)組print_r( $fiearr[1]);exit; }輸出結(jié)果:
Array([0] =>cat_html_path,cat_html_name|"funcname":"definefuc","conststr":"1"[1] =>cat_html_name[2] =>cat_name)
使用for循環(huán) , 得到value值,“|”分割數(shù)組判斷是否包含自定義函數(shù);
for($i=0;$i<=count($match[1])-1;$i){ $name="json_decode(" {".$match[1][$i]."}")-"="">name;$fielpreg="/{f".$name."(.*?)}/";preg_match_all($fielpreg, $match[2][$i], $fiearr);for($z=0;$z<=count($fiearr[1])-1;$z){$isarr=explode("|",$fiearr[1][$z]); 存在自定義方法if(count($isarr)="">1){$funcname=json_decode("{".$isarr[1]."}")->funcname;$conststr=json_decode("{".$isarr[1]."}")->conststr;$fiar=explode(",",$fiearr[0]);//可以是多個變量 , 合并成了字符串$fistr="";for($c=0;$c<=count($fiar)-1;$c){ $fistr.="$$name[$fiar[$c]]." ,";"="" }="" 左側(cè)參數(shù)="" $fistr="" 右側(cè)常量參數(shù)$conststr="" $fistr="mb_substr($fistr,0,mb_strlen($fistr)-1);" $str="str_replace($fiearr[0][$i],$funcname($fistr,$conststr),$str);}else{">=count($fiar)-1;$c){>,$str=str_replace($fiearr[0][$i],$$name[$fiearr[1][$z]],$str);}}}注意:
傳遞的變量參數(shù),多個參數(shù)實際是一個字符串 , 在我們自定義方法內(nèi)使用該參數(shù)的時候應(yīng)該是先分割成數(shù)組,單個參數(shù)沒有影響 。
=count($fiearr[1])-1;$z){$isarr=explode("|",$fiearr[1][$z]);>=count($match[1])-1;$i){>=count($match[1])-1;$i){>=count($match[1])-1;$i){>
猜你喜歡
- 49歲農(nóng)民如何交社保,五十歲農(nóng)村人怎么交社保
- 汽車換擋怎么操作
- 汽車怎么連接carplay車載
- 這5樣?xùn)|西會讓博美犬害怕 博美最怕主人怎么懲罰
- 情感老師小馮文本情感分析主要方法情感顧問免費(fèi)在線咨詢
- 我的自畫像400字作文免費(fèi)女孩 我的自畫像400字作文免費(fèi)沒想到
- 交通事故對方全責(zé)保險公司怎么賠償 交通事故對方全責(zé)保險公司如何賠償
- 金毛怎么訓(xùn)練大小便 如何訓(xùn)練金毛大小便
- 交通事故誤工費(fèi)怎么算 交通事故誤工費(fèi)如何算
- 怎么解除非法同居關(guān)系 非法同居應(yīng)該怎樣解除關(guān)系
