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

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作