正则表达式的经文匹配

时间:2015-06-10 20:11:14

标签: php regex preg-replace-callback

我有以下正则表达式代码,我正在努力完善圣经搜索。 ......是66本圣经书籍的其余部分。

/(?=\S)\b(genesis|exodus|leviticus|...)(\.)?(\s)?((\d{1,3})([:,-;]?(?=\d))?((\d{1,3})?([:,-;]?(?=\d))?){0,50})/iu

我遇到的问题是,有多个段落引用,如 创世记3:2 1历代志下2:2如果我在搜索中使用正则表达式并且还有一个空格,则该函数假设历代记录中的1是创世纪1并且不识别它并将其发送到preg_replace_callback。有什么建议吗?

输入为string,整个段落如

Mat 3:12; Luke 3:17; Revelation 16:14 • gathered, 7 Mat 2:4; Mat 22:34; Mat 27:27; Mar 4:1; Mar 5:21; Joh 11:47; Act 4:26 • into, 1 Revelation 13:10 • resorted, 1 Joh 18:2 • themselves, 1 Act 11:26 • together, 24 Mat 13:2; Mat 22:41; Mat 26:3; Mat 27:17; Mat 27:62; Mar 2:2; Mar 6:30; Mar 7:1; Luke 22:66; Joh 11:52; Act 4:6; Act 4:27; Act 4:31; Act 13:44; Act 14:27; Act 15:6; Act 15:30; Act 20:7; Act 20:8; 1 Corinthians 5:4; Revelation 16:16; Revelation 19:17; Revelation 19:19; Revelation 20:8

我要做的是使它与经文和段落相匹配,然后使用href函数将其转换为pre_replace_callback链接。

1 个答案:

答案 0 :(得分:2)

在您的情况下,您可以在评论中使用@ danielson317提供的regex并将其扩展以满足您的需求。它甚至会处理创世记3:2历代志下2:2&#34;正确地,给你<a href...>Genesis 3:2</a> <a href...>1 Chronicles 2:2</a>

你甚至可以把它变成一个类:

<?php
$scriptureUtils = new ScriptureUtils();
echo $scriptureUtils->addLinks($text);

class ScriptureUtils {

    private $booksAbbreviated = array(
        "Gen", "Exo", "Lev", "Num", "Deu", "Jos", "Jud", "Rut", "1 Sam", "2 Sam", "1 Kin", 
        "2 Kin", "1 Chr", "2 Chr", "Ezr", "Neh", "Est", "Job", "Psa", "Pro", "Ecc", "Son", 
        "Isa", "Jer", "Lam", "Eze", "Dan", "Hos", "Joe", "Amo", "Oba", "Jon", "Mic", 
        "Nah", "Hab", "Zep", "Hag", "Zec", "Mal", "Mat", "Mar", "Luk", "Joh", "Act", 
        "Rom", "1 Cor", "2 Cor", "Gal", "Eph", "Phi", "Col", "1 The", "2 The", "1 Tim", 
        "2 Tim", "Tit", "Phi", "Heb", "Jam", "1 Pet", "2 Pet", "1 Joh", "2 Joh", "3 Joh", 
        "Jud", "Rev"
    );

    private $booksFullnames = array(
        "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy", "Joshua", "Judges", 
        "Ruth", "1 Samuel", "2 Samuel", "1 Kings", "2 Kings", "1 Chronicles", 
        "2 Chronicles", "Ezra", "Nehemiah", "Esther", "Job", "Psalms", "Proverbs", 
        "Ecclesiastes", "Song of Solomon", "Isaiah", "Jeremiah", "Lamentations", "Ezekiel", 
        "Daniel", "Hosea", "Joel", "Amos", "Obadiah", "Jonah", "Micah", "Nahum", 
        "Habakkuk", "Zephaniah", "Haggai", "Zechariah", "Malachi", "Matthew", "Mark", 
        "Luke", "John", "Acts", "Romans", "1 Corinthians", "2 Corinthians", "Galatians", 
        "Ephesians", "Philippians", "Colossians", "1 Thessalonians", "2 Thessalonians", 
        "1 Timothy", "2 Timothy", "Titus", "Philemon", "Hebrews", "James", "1 Peter", 
        "2 Peter", "1 John", "2 John", "3 John", "Jude", "Revelation"
    );

    private $addLinkPattern;

    public function __construct() {
        $this->defineLinkPattern();
    }

    public function addLinks($text) {
        $returnString = preg_replace_callback(
            $this->addLinkPattern,
            array($this, "addLinkCallback"),
            $text
        );
        return $returnString;
    }

    private function addLinkCallback($matches) {
        $returnString = "";
        foreach($matches as $match) {
            $returnString .= "<a href=\"bible.php?passage=$match\">$match</a>";
        }
        return $returnString;
    }

    private function defineLinkPattern() {
        // It is important that the full names appear before the abbreviated ones.
        $bookList = implode("|", array_merge($this->booksFullnames, $this->booksAbbreviated));
        $this->addLinkPattern = "/\\b(?:$bookList)(?:\\s+\\d+)?(?::\\d+(?:–\\d+)?(?:,\\s*\\d+(?:–\\d+)?)*)?/";
    }

}