HTML解析-将文本转换为链接


HTML Parsing - Convert a Text into a Link

假设我有这样的文本:

亚伦与他哥哥在米利巴的罪有牵连(Num。20:8-13),因此不被允许进入应许之地。众支派到了以东地边的何珥山,摩西照神的吩咐,在众百姓眼前领亚伦和他儿子以利亚撒上那山。他在那里把亚伦的圣衣脱下来,给以利亚撒穿上。亚伦死在山顶上,年一百二十三岁。20:23-29 。Comp。。6 ; 32:50 )

我想做的是,将上面的每个粗体文本转换为一个链接,如果它是:

正文结构如下:

<DIV>
  <B>Aaron</B>
  <SPAN>
    Aaron was implicated in the sin of his brother at Meribah (Num. 20:8-13), and on that account was not permitted to enter the Promised Land. When the tribes arrived at Mount Hor, "in the edge of the land of Edom," at the command of God Moses led Aaron and his son Eleazar to the top of that mountain, in the sight of all the people. There he stripped Aaron of his priestly vestments, and put them upon Eleazar; and there Aaron died on the top of the mount, being 123 years old (Num. 20:23-29. Comp. Deut. 10:6; 32:50)
  </SPAN>
</DIV>

任何好的想法都会受到赞赏。谢谢:)


编辑

代码:

$chapters = array ("Deut", "Num");
$html = file_get_html($link);
foreach($html->find('div') as $dict) {
    $descr  = $dict->find('SPAN', 0)->innertext;    
    $descrl = preg_replace("/$chapters'. [0-9:-]*/", "<a href='"$0'">$0</a>", $descr); //--> See description below
    echo $descrl . "<hr/>";
}

描述:当我将$chapters更改为单个单词如NumDeut时,它工作得很好,但是当我将其更改为$chapters时,它不返回任何链接。

你没有指定规则,你应该自己定义和改进;你的具体情况我已经处理过了。

//replace against either book followed by period followed by space
//followed by one or more digit, comma, semicolon, space, or dash
txt.replace(/(Num|Deut)'. (['d:,; -]+)/g, function (match, book, verses) {
    var link = '';
    //split the verse on semicolon + space as each must be linked
    verses.split(/;'s+/).forEach(function (elem) {
        //create the link; replace : with period
        link += '<a href="' + book.toLowerCase() + elem.replace(':', '.') + '">'
            + book + '. ' + elem + '</a> ';
    });
    return link;
});
http://jsfiddle.net/XaVXW/