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
        );
?>
  • 写回答

1条回答 默认 最新

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

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

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据