php
  • regex
  • 2013-02-01 82 views 1 likes 
    1

    我正在處理一個正則表達式,但我無法修復它。正則表達式在PHP函數中查找信息

    我掃描與PHP文件名(.php)和我在尋找:$__this('[TEXT]')$__this("[TEXT]")

    所以我的問題是:有人可以幫我在一個字符串搜索一個正則表達式:$__this('[TEXT]')$__this("[TEXT]"),給我[TEXT]

    UPDATE(有答案,感謝@Explosion丸):

    $string = '$__this("Foo Bar<br>HelloHello")'; 
    preg_match('/\$__this\(([\'"])(.*?)\1\)/xi', $string, $matches); 
    print_r($matches); 
    
    +0

    即使返回文本,您仍然需要執行額外的處理來取回實際的字符串。 – nhahtdh

    +1

    您可以使用這個:http://www.php.net/manual/en/function.token-get-all.php將PHP文件標記爲PHP令牌。其餘的不應該那麼辛苦。 – nhahtdh

    +0

    您可能需要這樣的東西:\ $ __ this \(('|「)\ [(\ w +)\]('|」)\) – oopbase

    回答

    2
    preg_match('/ 
        \$__this # just $__this. $ is meta character and must be escaped 
        \(  # open paren also must be escaped 
        ([\'"]) # open quote (capture for later use). \' is needed in string 
        (\[  # start capture. open bracket must also be escaped 
        .*?  # Ungreedily capture whatever is between the quotes 
        \])  # close the open bracket and end capture 
        \1  # close the quote (captured earlier) 
        \)  # close the parentheses 
    /xi'   # ignore whitespace in pattern, allow comments, case insensitive 
    , $document, $matches); 
    

    捕獲的文本將在$matches[2]。這假定每行可能捕獲一次。如果您需要更多,請使用preg_match_all

    +1

    這項工作如果假設所有文本的格式爲'[something here]',但我認爲'[]'只是某種標記,表示該部分可以是任何東西。這一點上的問題還不清楚。 – nhahtdh

    +0

    @nhahtdh如果'[]'實際上不是字符串的一部分,那麼你可以簡單地將它們從我的模式中刪除。不過,我認爲他們是專門在那裏的。這是否使我的答案不值得讚賞? –

    +1

    如果文本中包含''「',並且你還刪除了'[]',那麼你的回答值得投票。 – nhahtdh

    0

    如何AB出:

    preg_match('/\$__this(?:(\'|")\((.+?)\)\1)/', $string); 
    

    解釋:

    (?-imsx:\$__this(?:(\'|")\((.+?)\)\1)) 
    
    matches as follows: 
    
    NODE      EXPLANATION 
    ---------------------------------------------------------------------- 
    (?-imsx:     group, but do not capture (case-sensitive) 
             (with^and $ matching normally) (with . not 
             matching \n) (matching whitespace and # 
             normally): 
    ---------------------------------------------------------------------- 
        \$      '$' 
    ---------------------------------------------------------------------- 
        __this     '__this' 
    ---------------------------------------------------------------------- 
        (?:      group, but do not capture: 
    ---------------------------------------------------------------------- 
        (      group and capture to \1: 
    ---------------------------------------------------------------------- 
         \'      ''' 
    ---------------------------------------------------------------------- 
        |      OR 
    ---------------------------------------------------------------------- 
         "      '"' 
    ---------------------------------------------------------------------- 
        )      end of \1 
    ---------------------------------------------------------------------- 
        \(      '(' 
    ---------------------------------------------------------------------- 
        (      group and capture to \2: 
    ---------------------------------------------------------------------- 
         .+?      any character except \n (1 or more 
               times (matching the least amount 
               possible)) 
    ---------------------------------------------------------------------- 
        )      end of \2 
    ---------------------------------------------------------------------- 
        \)      ')' 
    ---------------------------------------------------------------------- 
        \1      what was matched by capture \1 
    ---------------------------------------------------------------------- 
    )      end of grouping 
    ---------------------------------------------------------------------- 
    )      end of grouping 
    ---------------------------------------------------------------------- 
    
    +0

    你錯過了括號和括號 –

    0

    下面是一個解決方案,它將捕獲帶有引號和撇號的字符串。

    $txt = " 
    blah blah blah 
    blah \$_this('abc') blah 
    blah \$_this('a\"b\"c') blah balah \$_this('a\"b\"c\'') 
    \$_this(\"123\");\$_this(\"1'23\") \$_this(\"1'23\\\"\") 
    "; 
    
        $matches = array(); 
        preg_match_all('/(?:\$_this\()(?:[\'"])(.*?[^\\\])(?:[\'"])(?:\))/im', $txt, $matches); 
        print_r($matches[1]); 
    
    相關問題