Regex to parse define() contents, possible?

前端 未结 5 1943
北海茫月
北海茫月 2021-01-13 11:06

I am very new to regex, and this is way too advanced for me. So I am asking the experts over here.

Problem I would like to retrieve the constants /

5条回答
  •  情深已故
    2021-01-13 11:15

    You might not need to go overboard with the regex complexity - something like this will probably suffice

     /DEFINE\('(.*?)',\s*'(.*)'\);/
    

    Here's a PHP sample showing how you might use it

    $lines=file("myconstants.php");
    foreach($lines as $line) {
        $matches=array();
        if (preg_match('/DEFINE\(\'(.*?)\',\s*\'(.*)\'\);/i', $line, $matches)) {
            $name=$matches[1];
            $value=$matches[2];
    
            echo "$name = $value\n";
        }
    
    }
    

提交回复
热议问题