doubu5035 2018-05-02 10:34 采纳率: 100%
浏览 53
已采纳

PHP正则表达式匹配所有圣经引用不起作用

I'm currently struggling with a regular expression to match all bible quotes in body text so I can convert them into links. I was hoping someone could please help out with this issue as I'm new to regex. So far I am using this:

/([^(])\w+ +\d+:+[\d+[?\-]+([^)])/

Some example text looks like this:

    Lorem ipsum dolor sit amet, sea persius nonumes habemus cu. Eam ferri dicta ex, sit no torquatos reprimique. Ea tota democritum has, mel libris audiam voluptaria cu. Ex usu facer voluptatibus, nam ut commune democritum. Ea nonumes  (1 John 3:1) recteque sapientem eum. Cum soleat mandamus te, (Matthew 6:6) eos affert volutpat et.

Quo ei iracundia consectetuer. Vim salutatus abhorreant at, quo ea postea equidem intellegam. Duo dicit cotidieque id, debitis noluisse fabellas sit eu. Qui ne integre patrioque, minimum inciderint eos ex, aliquando (Deuteronomy 10:18; Psalm 10:14) tincidunt intellegat est at. Sumo assum et nec, usu graecis adversarium te, cu mea quis lorem. In qui utroque propriae, ex mandamus argumentum vis, pro vidisse ceteros atomorum ex. Et mentitum referrentur

My regex is individually matching most of the quotes in my pages so far, including the example text but(1 John 3:1) and (Matthew 6:6) don't work. The regex needs to match an opening brace and omit it from the resulting string then match an optional number with a space and or a word (book name) followed by a space and a number (chapter) followed by a colon (:) then a number with an optional dash (-) and another number (verses), followed by an optional semicolon (;) if there are multiple references, followed by a closing brace (omit this from the resulting match). What do I update in order to match my desire criteria?

Example code:

<?php
$body = "Lorem ipsum dolor sit amet, sea persius nonumes habemus cu. Eam ferri dicta ex, sit no torquatos reprimique. Ea tota democritum has, mel libris audiam voluptaria cu. Ex usu facer voluptatibus, nam ut commune democritum. Ea nonumes  (1 John 3:1) recteque sapientem eum. Cum soleat mandamus te, (Matthew 6:6) eos affert volutpat et.

        Quo ei iracundia consectetuer. Vim salutatus abhorreant at, quo ea postea equidem intellegam. Duo dicit cotidieque id, debitis noluisse fabellas sit eu. Qui ne integre patrioque, minimum inciderint eos ex, aliquando (Deuteronomy 10:18; Psalm 10:14) tincidunt intellegat est at. Sumo assum et nec, usu graecis adversarium te, cu mea quis lorem. In qui utroque propriae, ex mandamus argumentum vis, pro vidisse ceteros atomorum ex. Et mentitum referrentur";

    $template_body = preg_replace_callback(
            '/([^(])\w+ +\d+:+[\d+[?\-]+([^)])/',
            function ($matches) {
                $replacement = '<a href="https://www.biblegateway.com/passage/?search='.$matches[0].';&version=NIVUK">'.$matches[0].'</a>';
                return $replacement;
            },
            $body
        );
?>
  • 写回答

2条回答 默认 最新

  • douyun7285 2018-05-02 10:57
    关注

    What about: [ (](([0-9] )?[\w]+ [0-9]{1,2}:[0-9]{1,2})[;)] ?

    https://regex101.com/r/Y4nasm/3

    Details

    [ (]: match a space or (

    ([0-9] )?: optionally match a number followed by a space

    [\w]+: matches any word character

    [0-9]{1,2}: match a number of length 1 or 2

    [;)]: match a ; or (

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况