[英]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.