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

时间:2014-01-13 03:54:04

标签: php preg-match-all

我有一个看起来像这样的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].'|';
}

但是我如何用“&amp;”分隔两个不同的感觉区域? (或任何分开的标记)?

3 个答案:

答案 0 :(得分:0)

使用DOMDocument班级。它非常简单!

[Also don't try to parse HTML with 'Regex'. It is not adviced]

<?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>

答案 1 :(得分:0)

正如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>

答案 2 :(得分:0)

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

$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>