我正在与Laravel一起开发一种页面生成器,并try 实现WordPress风格的短代码.我有一个字符串形式的HTML代码,如下所示:

$string = '<div class="temp">
    [title type="text" tag="h2" value="This is my cool title" disabled some-attribute]
    [text type="text" tag="p" value="Lorem ipsum dolor sit amet"]
</div>';

好的,现在我想将所有的"短码"(方括号[...]中的内容)提取并存储在一个数组中,如下所示:

[
  [
   "id" => "title",
   "type" => "text",
   "tag" => "h2",
   "value" => "This is my cool title",
   "disabled" => true,
   "some-attribute" => true
  ],
  [
   "id" => "text",
   "type" => "text",
   "tag" => "p",
   "value" => "Lorem ipsum dolor sit amet"
  ]
]

我的问题是将短码字符串分解成一个数组,因为""中有空格.

以下是我的PHP代码:

// Extract all brackets from the string
preg_match_all('/(?<=\[)[^\][]*(?=])/', $string, $matches);

$itemsArray = array();
// Explode each shortcode by spaces
foreach ($matches[0] as $match) {
    $shortcode = explode( ' ', $match );
    $itemArray = array();
    // split shortcode in attributes
    foreach ($shortcode as $key => $attr) {
        $temp = explode('=', $attr);
        // First attribute is the id
        if ($key === 0) {
            $itemArray['id'] = $temp[0];
        } else {
            if ( count($temp) > 1 ) {
                $itemArray[ $temp[0] ] = trim($temp[1],'"');
            } else {
                $itemArray[ $temp[0] ] = true;
            }
        }
    }
    $itemsArray[] = $itemArray;
}

结果是接近,但引号""中的空格搞砸了字符串的分解:

[
  [
    "id" => "title"
    "type" => "text"
    "tag" => "h2"
    "value" => "This"
    "is" => true
    "my" => true
    "cool" => true
    "title"" => true
    "disabled" => true
    "some-attribute" => true
  ],
  [
    "id" => "text"
    "type" => "text"
    "tag" => "p"
    "value" => "Lorem"
    "ipsum" => true
    "dolor" => true
    "sit" => true
    "amet"" => true
  ]
]

如何排除字符串中的空格EXPLAGE?

推荐答案

如果查看您的字符串,除了DIV中以[开头和以]结尾的项之外,它们与标准的HTMLDOM元素几乎相同,因此将这些行转换为HTML然后通过DOMDocument访问可能会更容易.


试一下这个快速的例子

如果您知道这些值永远不会包含方括号,您可以try 通过PHP的DOMDocument特性以这种方式获取数据.

// Your string
$string = '<div class="temp">
    [title type="text" tag="h2" value="This is my cool title" disabled some-attribute]
    [text type="text" tag="p" value="Lorem ipsum dolor sit amet"]
</div>';

// Set all the '[word ' items as pretend <span id='word'
$string = preg_replace("/\[([a-z]*.?) /i","<span id=\"\$1\" ",$string);

// Replace the outer ] with '>' to make them all appear to be HTML tags
$string = str_replace("]",">",$string);

// Start a DOMDocument instance
$dom = new DOMDocument;

// Load $string now that it resembles full HTML
$dom->loadHTML($string);
$array = Array();

// Loop through the span elements
foreach($dom->getElementsByTagName('span') as $node){
    $attrs = Array();

    // Collect their attributes
    foreach($node->attributes as $attr){
        $attrs[$attr->nodeName] = $attr->nodeValue;
    }
    $array[] = $attrs;
}

// Show the results
echo "<textarea style='width:100%;height:500px'>";
print_r($array);
echo "</textarea>";

它可能需要调整一下. 正如我所说的,通过[和]捕获元素可能需要一种更严格的方法来实现,以避免在值中包含"]"时出现任何令人讨厌的意外.

我希望这对我的 idea 有一点帮助.

Php相关问答推荐

PHP在将数组子集赋给变量时是否复制数组子集?

获得客户S在WooCommerce为期1年的总支出

使用.htaccess将任何路由重定向到文件

从WooCommerce订单状态更改更新用户自定义元数据

将一个情态形式放在细丝v3中的形式中间

这是在可召回的背景下吗?

如何在execute语句中使用存储过程参数?

Laravel服务Provider 没有向服务类注入价值

在个人资料页面上显示个人资料图像

PHP SimpleXML:按属性值访问 node

Wordpress,配置一周第一天选项

通过存储库检索 Blade 上的单值数据 出错

根据WooCommerce中选定的变体重量更改输入数量步值

Laravel使用另一张表进行关联查询

在 Symfony 测试中何时使用 TestCase 而不是 KernelTestCase

PHP 将数组值插入到 csv 列

使用 RSA 2048 公钥验证 RSA PKCS#1v1.5 SHA 256 签名

Laravel Websockets 错误:(WebSocket 在建立连接之前关闭)

为什么非贪婪匹配会消耗整个模式,即使后面跟着另一个非贪婪匹配

图像不会显示在生产中的 Laravel 应用程序中