0

假设我有这个文本:

亚伦与他兄弟在米利巴的罪有牵连(民数记 20:8-13),因此不被允许进入应许之地。当部落到达何珥山时,“在以东地的边缘”,摩西在上帝的命令下带领亚伦和他的儿子以利亚撒到了山顶,在所有人的视线中。他在那里脱下亚伦的祭司袍,给以利亚撒穿上;亚伦死在山顶上,享年 123 岁(民数记 20:23-29。比较申命记 10:632 :50 )

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

  • 编号。20:8-12,应该是:<a href="num20.8-12">Num. 20:8-13</a>
  • 申命记。10:6;32:50,应该是:<a href="deut10.6">Deut. 10:6</a> <a href="deut32.50">申命记。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为单个单词,例如Numor Deut,但效果很好,但是当我将其更改为$chapters时,它不会返回任何链接。

4

1 回答 1

2

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

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

于 2013-03-29T03:48:58.943 回答