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/

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

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来