Home >Backend Development >PHP Tutorial >一个php变量封装进数组的有关问题
一个php变量封装进数组的问题
遇到一个关于json的问题。有一组正则规则,由于有很多行(以后可能还需要增删)
所以在主页页面里用一个固定的foreach代码,然后将正则规则封装一个json数组,保存到外部文件(以后修改规则时只需修改这个外部文件)。
json.txt代码
$json=<<<EOT<br />[{"a":"!preg_match([email protected]\\\.aaa\\\.com@',$url,$match)||!preg_match([email protected]\\\.bbb\\\.com@',$url,$match)||!preg_match([email protected]\\\.ccc\\\.com@',$url,$match)","b":"1"},{"a":"!preg_match([email protected]\\\.ddd\\\.com@',$url,$match)||!preg_match([email protected]\\\.eee\\\.com@',$url,$match)","b":"2"},{"a":"!preg_match([email protected]\\\.fff\\\.com@',$url,$match)||!preg_match([email protected]\\\.ggg\\\.com@',$url,$match)","b":"3"}]<br />EOT;<br />
<br />include("json.txt");<br />$date = json_decode($json);<br />foreach($date as $row){<br /> if($row->a){<br /> //做一些$row->b的事物<br /> }<br />}