duang5049 2013-03-29 01:42
浏览 33
已采纳

HTML解析 - 将文本转换为链接[关闭]

Supposed that I have this text:

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)

What I want to do is, to convert every bold text above into a link, and the link, if it is:

  • Num. 20:8-12, should be like: < a href="num20.8-12">Num. 20:8-13< /a>
  • Deut. 10:6; 32:50, should be like: < a href="deut10.6">Deut. 10:6< /a> < a href="deut32.50">Deut. 32:50< /a>

The structure of this text is like below:

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

Any great ideas would be appreciated. Thanks :)


EDIT

The code:

$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/>";
}

Description: While I change the $chapters into a single word like Num or Deut, it works well, but while I change it into $chapters, it doesn't returns any link.

  • 写回答

1条回答 默认 最新

  • doumor942473 2013-03-29 03:48
    关注

    You did not specify the rules, which you should define and improve for yourself; I've handled your specific case.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀