解析数组中的 BBCode

Parse BBCode in array

我正在尝试从 BBCode(如 WordPress 短代码)调用一个函数。但我没有找到任何代码来做到这一点,只有我发现 HTML 标签解析器,如:

[bold]Bold text[/bold]
->
<b>Bold text</b>

但是我想保存为数组,例如:

[date format="j M, Y" type="jalali"]

像这样:

array(
    'date' => array(
        'format' => 'j M, Y',
        'type' => 'jalali'
    )
)

*已编辑

我编写了一个代码来执行此操作(抱歉,如果我的英语不好):

[date format="Y/m/d" type="jalali"] =>

Step 1: Get code between "[" and "]":
date format="Y/m/d" type="jalali"

Step 2: Explode space in the code:
$code = array('date', 'format="Y/m/d"', 'type="jalali"')

Step 3: Get shortcode name(offset 0 of $code) and get
difference($code excluded offset 0):
$name = 'date'
$attr = array('format="Y/m/d"', 'type="jalali"')

Step 4: Now I have attributes and code name. But the problem is if
put space in attributes value it will explode that too:
[date format="j M, Y" type="jalali"] =>
$code = array('date', 'format="j', 'M,', ' Y"', 'type="jalali"');

现在我该如何解决这个问题或使用正则表达式或其他任何方式获取名称和属性?

您可以使用正则表达式进行尝试

$code = '[date format="j M, Y" type="jalali"]';

preg_match_all("/\[([^\]]*)\]/", $code, $matches);

$codes = [];

foreach($matches[1] as $match) {
  // Normalize quotes into double quotes
  $match = str_replace("'",'"',$match);
  // Split by space but ignore inside of double quotes
  preg_match_all('/(?:[^\s+"]+|"[^"]*")+/',$match,$tokens);
  $parsed = [];
  $prevToken = '';
  foreach($tokens[0] as $token) {
    if(strpos($token,'=') !== false) {
      if($prevToken !== '') {
        $parts = explode('=',$token);
        $parsed[$prevToken][$parts[0]] = trim($parts[1],'"\''); 
      }
    } else {
      $parsed[$token] = [];
      $prevToken = $token;
    }
  }

  $codes[] = $parsed;
}

var_dump($codes);

结果:

array(1) {
  [0]=>
  array(1) {
    ["date"]=>
    array(2) {
      ["format"]=>
      string(6) "j M, Y"
      ["type"]=>
      string(6) "jalali"
    }
  }
}