2013-02-01 2 views
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 \ (('') \ [(\) +] \) – 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

모든 텍스트의 형식이'[여기에 있습니다]'라고 가정하면이 작업이 가능하지만'[]'은 그 부분이 무엇이든 될 수 있다고 말하는 표시 일뿐입니다. 이 점에 관해서는 의문이있다. – 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]);