繁体   English   中英

PHP正则表达式,在新字符串中分隔两个匹配项

[英]PHP regular expression, separate two matches in new string

我有一个看起来像这样的XML。 我已经将其加载到PHP的字符串中:

<sense>
<gloss>there</gloss>
<gloss>over there</gloss>
<gloss>that place</gloss>
<gloss>yonder</gloss>
</sense>
<sense>
<gloss>that far</gloss>
<gloss>that much</gloss>
<gloss>that point</gloss>
</sense>

我正在尝试将其格式化为如下所示:

<sense>
<gloss>there|over there|that place|yonder&that far|that much|that point</gloss>
</sense>

我几乎用以下代码做到了这一点:(也许有一种更聪明的方法,但是仍然...)

preg_match_all('~<gloss>(.*)</gloss>~sU', $input, $matches);

$newStr = '';
//Add all new matches and put them in a new string
for ($i=0; isset($matches[1][$i]); $i++)
{
    $newStr .= $matches[1][$i].'|';
}

但是,我如何用“&”(或任何分隔符号)分隔两个不同的感应域?

利用DOMDocument类。 很简单!

[[也不要尝试使用'Regex'解析HTML。 不建议]

<?php
$html='<sense>
<gloss>there</gloss>
<gloss>over there</gloss>
<gloss>that place</gloss>
<gloss>yonder</gloss>
</sense>
<sense>
<gloss>that far</gloss>
<gloss>that much</gloss>
<gloss>that point</gloss>
</sense>';
$dom = new DOMDocument;
@$dom->loadHTML($html);

foreach ($dom->getElementsByTagName('sense') as $tag) {
    foreach($tag->getElementsByTagName('gloss') as $intag )
    {
    $str.=$intag->nodeValue."|";
    }
    $str= rtrim($str,'|');
    $str.="&";

}

echo "<sense><gloss>".rtrim($str,'&')."</gloss></sense>";

产量

there|over there|that place|yonder&that far|that much|that point

如果您查看源代码,则可以找到以下内容:

<sense><gloss>there|over there|that place|yonder&that far|that much|that point</gloss></sense>

正如kuroi的评论所说,xml库可能最适合这里的工作。 这可能不是最有效的代码,但是非常简单易用。

$xml = simplexml_load_string('
    <root>
        <sense>
            <gloss>there</gloss>
            <gloss>over there</gloss>
            <gloss>that place</gloss>
            <gloss>yonder</gloss>
        </sense>
        <sense>
            <gloss>that far</gloss>
            <gloss>that much</gloss>
            <gloss>that point</gloss>
        </sense>
    </root>
');

$senses = array();
foreach ($xml->sense as $sense) {
    $glosses = array();
    foreach ($sense->gloss as $gloss) {
        $glosses[] = (string) $gloss;
    }
    $senses[] = implode('|', $glosses);
}

$result = '<sense>'.implode('</sense><sense>', array_map('htmlspecialchars', $senses)).'</sense>';

将返回$ result:

<sense>there|over there|that place|yonder</sense><sense>that far|that much|that point</sense>

将您的字符串分解为两个数组,然后使用正则表达式对其进行查看:

$text = "<sense>
<gloss>there</gloss>
<gloss>over there</gloss>
<gloss>that place</gloss>
<gloss>yonder</gloss>
</sense>
<sense>
<gloss>that far</gloss>
<gloss>that much</gloss>
<gloss>that point</gloss>
</sense>";
$string = array();
array_walk((explode("<sense>", $text)), function($part) use (&$string)
{
    preg_match_all("@<gloss>(.*?)</gloss>@", $part, $match);
    count($match[1]) > 0 ? $string[] = implode("|", $match[1]) : null;
});
echo "<sense><gloss>".implode("&", $string)."</gloss></sense>";

输出

<sense><gloss>there|over there|that place|yonder&that far|that much|that point</gloss></sense>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM