0

I have one page, there would be have some regex work. cause the regex rule would be added or removed in the future. I'd like to write them into an out file. cause the here regex rule is very long, in my way i wrote there into a json data, but the php variable is un-worked. Is there anyone could give me some good way for a work solution? Thanks.

json.txt

$json=<<<'EOT'
[
    {
        "a": "!preg_match('@www\\.aaa\\.com@',$url,$match)&&!preg_match('@www\\.bbb\\.com@',$url,$match)&&!preg_match('@www\\.ccc\\.com@',$url,$match)",
        "b": "1"
    },
    {
        "a": "!preg_match('@www\\.ddd\\.com@',$url,$match)&&!preg_match('@www\\.eee\\.com@',$url,$match)",
        "b": "2"
    },
    {
        "a": "!preg_match('@www\\.fff\\.com@',$url,$match)",
        "b": "3"
    }
]
EOT;

main.php

include("json.txt");
$date = json_decode($json);
foreach($date as $row){
    if($row->a){// i'd like to make some judge like if(!preg_match('@www\\.fff\\.com@',$url,$match)), but now the $url make non-sence, un-worked `php variable`
       echo $row->b.'<hr />';
    }
}
4

1 に答える 1

0

この方法を試してみることもできますeval(こちらのドキュメントを参照してください: http://php.net/manual/en/function.eval.php )

if(eval("return $row->a;")){

ただし、ドキュメントに記載されているように、あなた以外の誰かが json.txt ファイルを編集できる場合、これは危険な方法です。$row->aしたがって、このソリューションを試す前に、許可された人だけがコンテンツを変更できることを確認してください。

于 2013-01-29T14:39:53.703 に答える