smarty模板中判斷一個字符包含在一個字符串裏

$key要查找的字符,

$strArr數組,

{if in_array($key,$strArr)}
輸出
{/if}

模板裏判斷的,兩個變量:
{$key}
{$str}
其中
$key=”1″;
$str=”,1,3,5,”;
$strArr = explode(“,”, $str);

在模板中使用格式:
{if in_array($key, $strArr)}Ok{else}Not in!{/if}

 

應用舉例:

 <input name="purchase_category[]" type="checkbox" value="tea" {if in_array("tea", explode(",",$distributor.purchase_category))}checked="checked"{/if} />茶葉
                      <input name="purchase_category[]" type="checkbox" value="tea_tool"  {if in_array("tea_tool", explode(",",$distributor.purchase_category))}checked="checked"{/if} />茶具
                      <input name="purchase_category[]" type="checkbox" value="tea_food" {if in_array("tea_food", explode(",",$distributor.purchase_category))}checked="checked"{/if} />茶食品
                      <input name="purchase_category[]" type="checkbox" value="tea_use" {if in_array("tea_use", explode(",",$distributor.purchase_category))}checked="checked"{/if} />茶日用品
                      <input name="purchase_category[]" type="checkbox" value="tea_pack"  {if in_array("tea_pack", explode(",",$distributor.purchase_category))}checked="checked"{/if}/>茶包裝
                      <input name="purchase_category[]" type="checkbox" value="other" {if in_array("other", explode(",",$distributor.purchase_category))}checked="checked"{/if} />其他

發佈了23 篇原創文章 · 獲贊 4 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章