Php Preg_Split没有保留delimeter

Php Preg_Split没有保留delimeter,php,regex,preg-split,Php,Regex,Preg Split,我试图从我拥有的一个短代码标记中解析属性。我开始使用的字符串看起来像这样 href="https://example.com" class="bootstrap class names" 我用了这个表达式,这让我几乎达到了目的 $attribute_dna = '/"\s/'; $attributes = preg_split($attribute_dna, $attributes_string, null, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMP

我试图从我拥有的一个短代码标记中解析属性。我开始使用的字符串看起来像这样

href="https://example.com" class="bootstrap class names"
我用了这个表达式,这让我几乎达到了目的

$attribute_dna = '/"\s/';
$attributes = preg_split($attribute_dna, $attributes_string, null, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
我得到的结果是

string(27) "href="https://example.com"  <--Problem is right here (missing a quote)
string(17) "class="bootstrap class names""

string(27)“href=”https://example.com“这不是更漂亮吗

<?php

$html = <<<DATA
<div>
    <a href="https://example.com" class="bootstrap class names">Some link here</a>
</div>
DATA;

# set up the DOM
$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);

# set up the xpath
$xpath = new DOMXPath($dom);

foreach ($xpath->query("//a") as $link) {
    foreach ($link->attributes as $attr) {
        $name = $attr->nodeName;
        $value = $attr->nodeValue;
        echo "Attribute '$name': '$value'\n";
    }
}
?>

这不是更漂亮吗

<?php

$html = <<<DATA
<div>
    <a href="https://example.com" class="bootstrap class names">Some link here</a>
</div>
DATA;

# set up the DOM
$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);

# set up the xpath
$xpath = new DOMXPath($dom);

foreach ($xpath->query("//a") as $link) {
    foreach ($link->attributes as $attr) {
        $name = $attr->nodeName;
        $value = $attr->nodeValue;
        echo "Attribute '$name': '$value'\n";
    }
}
?>

你可以使用.Split on
(?可能重复@ctwheels是的,他应该使用一个库来使用这个,但这不是他问的问题。顺便说一句-链接的重复答案质量很差。。你可以使用.Split on
(?可能重复@ctwheels是的,他应该使用一个库来使用它,但这不是他问的问题。顺便说一句,链接的重复答案质量很差。为什么要向下投票?这是解析XML或HTML的正确方法。你不使用正则表达式来做这件事。看起来OP没有HTML,但有一些简短的代码,如question@PeeHaa
我正试图从一个短代码标记中解析属性
-很明显OP正试图这样做。@ctwheels:我也这么想。即使OP有HTML(这是你的一个很大的假设)您正在做的事情不需要xpath为什么要进行向下投票?这是解析XML或HTML的正确方法。您不需要使用正则表达式来完成此操作。看起来OP没有HTML,但有一些简短的代码,如中所述question@PeeHaa
我正试图从一个短码标记中解析属性
-很明显,OP正试图这样做。@ctwheels:I我也这么认为。即使OP有HTML(这是您的一个很大的假设),您所做的事情也不需要xpath