用正则表达式识别数组或字符串?

时间:2016-02-25 16:06:10

标签: php regex

我有一个这种结构的文件:

[25-02-2016 16:55:09.497203] [exception] SQLSTATE[42S02]: Base table or 
view not found: 1146 Table 'test.pdd_userss' doesn't exist 
   0: array(
      'Stack' => 'exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.pdd_userss' doesn't exist' in C:\Bitnami\wampstack-5.5.29-1\apache2\htdocs\api\v1\boot\core\database.php:57
   Stack trace:
   #0 C:\Bitnami\wampstack-5.5.29-1\apache2\htdocs\api\v1\boot\core\database.php(57): PDO->prepare('SELECT * FROM p...')
   #1 C:\Bitnami\wampstack-5.5.29-1\apache2\htdocs\api\v1\boot\models\tests_model.php(31): Database->select('SELECT * FROM p...')
   #2 C:\Bitnami\wampstack-5.5.29-1\apache2\htdocs\api\v1\boot\controllers\test.php(43): Tests_Model->selectData()
   #3 C:\Bitnami\wampstack-5.5.29-1\apache2\htdocs\api\v1\boot\core\router.php(158): Test->selectUser()
   #4 C:\Bitnami\wampstack-5.5.29-1\apache2\htdocs\api\v1\boot\core\map.php(52): Router->submit()
   #5 C:\Bitnami\wampstack-5.5.29-1\apache2\htdocs\api\v1\boot\system\api.php(107): Map->init_get()
   #6 C:\Bitnami\wampstack-5.5.29-1\apache2\htdocs\api\v1\boot\boot.php(21): Api->init()
   #7 C:\Bitnami\wampstack-5.5.29-1\apache2\htdocs\api\v1\index.php(18): Boot->__construct()
   #8 {main}',
      'ID' => 'john foo'
   )
[25-02-2016 16:55:09.497857] [info] Sistema executed. 
   0: array(
      'ID' => 'john foo'
   )
[25-02-2016 16:55:13.255716] [info] System done. 

现在对于前两种情况我没有问题反序列化结构如下(前两个跟踪)第三个跟踪没有被打印我在示例中把它放在我想要实现的结构:< / p>

{
"trace": [2]
0:  {
"date": "25-02-2016 17:02:31.830305"
"type": "exception"
"message": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.pdd_userss' doesn't exist"
"ID": "" <- THE ID ISN'T DISPLAYED
}-
1:  {
"date": "25-02-2016 17:02:31.831140"
"type": "info"
"message": "system done."
"ID": "john foo"
}-
-
 .... OTHER CONTENT
}

但是对于error,不显示任何内容,导致regex无法反序列化数据。这是我的代码:

 $fh = file_get_contents($file);

 if(!$fh) { throw new Exception("Not found"); }

 $content = array(); 
 $content["trace"] = array();

 preg_match_all('/\[(.*)\][\s]*?\[(.*?)\][\s]*?(.*)[\s.]+(?:\d+[^\']*\'ID\'[ ]*=>[ ]*\'(.*)\')?/',
 $fh, $regs, PREG_PATTERN_ORDER);

 for($i = 0; $i < count($regs[0]); $i++)
 {
       $content['trace'][] = array(
       'date'    => $regs[1][$i],
       'type'    => trim($regs[2][$i]),
       'message' => trim($regs[3][$i]),
       'ID'      => trim($regs[4][$i]),
            );
  }

  return $content;

如何在第一种情况下看到我有这样的数组:

0: array(
  'ID' => 'john foo'

并且事实上id正确显示,但在第二种情况下我有一个简单的字符串:'ID' => 'john foo'

字符串没有显示,所以我怎么能识别这两种情况?

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找的只是一个简单的is_array就在你的正则表达式之前...然后你可以编写一个if语句来处理它是否是一个数组......

  $content = array(); 
  $content["trace"] = array();

  if (is_array($regs)) {
  // it's an array.
  } else {
  // not an array.
  }
  ...etc....

编辑:哎呀...我应该在正则表达式之后说。在查看php文档之后,直到之后才会创建所需的数组。

preg_match_all('/\[(.*)\][\s]*?\[(.*?)\][\s]*?(.*)[\s.]+(?:\d+[^\']*\'ID\'[ ]*=>[ ]*\'(.*)\')?/', $fh, $regs, PREG_PATTERN_ORDER);

  if (is_array($regs)) {
  // it's an array.
  } else {
  // not an array.
  }
  ...etc....

您也可以尝试

if (empty($regs[4][0])) {} 

并从那里处理一组不同的正则表达式。