program story

PHP : 괄호 안의 텍스트를 추출하는 가장 좋은 방법은 무엇입니까?

inputbox 2020. 11. 4. 07:56
반응형

PHP : 괄호 안의 텍스트를 추출하는 가장 좋은 방법은 무엇입니까?


괄호 사이에 텍스트 세트를 추출하는 가장 좋은 / 가장 효율적인 방법은 무엇입니까? 가능한 가장 효율적인 방법으로 "이 (텍스트)를 제외한 모든 것을 무시"문자열에서 "텍스트"문자열을 얻고 싶다고 가정 해보십시오.

지금까지 내가 생각 해낸 최고는 다음과 같습니다.

$fullString = "ignore everything except this (text)";
$start = strpos('(', $fullString);
$end = strlen($fullString) - strpos(')', $fullString);

$shortString = substr($fullString, $start, $end);

이 작업을 수행하는 더 좋은 방법이 있습니까? 일반적으로 정규식을 사용하는 것이 효율성이 떨어지는 경향이 있다는 것을 알고 있지만 함수 호출 수를 줄일 수 없다면 이것이 가장 좋은 방법일까요? 생각?


나는 정규식을하고 그것을 극복 할 것입니다. 엄청난 성능 문제가 될만큼 충분한 반복 작업을 수행하지 않는 한 코딩이 더 쉽습니다 (그리고 되돌아 보면 이해할 수 있음).

$text = 'ignore everything except this (text)';
preg_match('#\((.*?)\)#', $text, $match);
print $match[1];

따라서 실제로 게시 한 코드는 작동하지 않습니다. substr()'s매개 변수는 $ string, $ start 및 $ length 이고 strpos()'s매개 변수는 $haystack, $needle입니다. 약간 수정 :

$ str = "이 (텍스트)를 제외한 모든 것을 무시합니다";
$ start = strpos ($ str, '(');
$ end = strpos ($ str, ')', $ start + 1);
$ length = $ end-$ start;
$ result = substr ($ str, $ start + 1, $ length-1);

약간의 미묘함 : 두 번째 괄호 $start + 1에서 strpos()검색 을 수행하는 동안 PHP를 돕기 위해 offset 매개 변수에 사용 했습니다 . 우리는 증가 $start하나를 줄일 $length경기에서 괄호를 제외 할 수 있습니다.

또한,이 코드에는 오류 검사 가 없습니다 . 를 수행하기 전에 확인 $start하고 $end=== false가 아닌지 확인 하고 싶을 것 substr입니다.

strpos/substr정규식 대 사용에 관해서는 ; 성능면에서이 코드는 정규 표현식을 능가합니다. 그래도 조금 더 말이 있습니다. 나는 먹고 숨을 쉬기 strpos/substr때문에 너무 신경 쓰지 않지만 다른 누군가는 정규식의 간결함을 선호 할 수 있습니다.


정규식을 사용하십시오.

if( preg_match( '!\(([^\)]+)\)!', $text, $match ) )
    $text = $match[1];

이것은 '['와 ']'사이의 모든 텍스트를 추출하여 2 개의 개별 배열 (즉, 한 배열의 괄호 안의 텍스트와 다른 배열의 괄호 밖의 텍스트)을 저장하는 샘플 코드입니다.

   function extract_text($string)
   {
    $text_outside=array();
    $text_inside=array();
    $t="";
    for($i=0;$i<strlen($string);$i++)
    {
        if($string[$i]=='[')
        {
            $text_outside[]=$t;
            $t="";
            $t1="";
            $i++;
            while($string[$i]!=']')
            {
                $t1.=$string[$i];
                $i++;
            }
            $text_inside[] = $t1;

        }
        else {
            if($string[$i]!=']')
            $t.=$string[$i];
            else {
                continue;
            }

        }
    }
    if($t!="")
    $text_outside[]=$t;

    var_dump($text_outside);
    echo "\n\n";
    var_dump($text_inside);
  }

출력 : extract_text ( "hello how are you?"); 다음을 생성합니다.

array(1) {
  [0]=>
  string(18) "hello how are you?"
}

array(0) {
}

extract_text ( "안녕하세요 [http://www.google.com/test.mp3] 잘 지내세요?"); 생산할 것이다

array(2) {
  [0]=>
  string(6) "hello "
  [1]=>
  string(13) " how are you?"
}


array(1) {
  [0]=>
  string(30) "http://www.google.com/test.mp3"
}

이 기능이 유용 할 수 있습니다.

    public static function getStringBetween($str,$from,$to, $withFromAndTo = false)
    {
       $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
       if ($withFromAndTo)
         return $from . substr($sub,0, strrpos($sub,$to)) . $to;
       else
         return substr($sub,0, strrpos($sub,$to));
    }
    $inputString = "ignore everything except this (text)";
    $outputString = getStringBetween($inputString, '(', ')'));
    echo $outputString; 
    //output will be test

    $outputString = getStringBetween($inputString, '(', ')', true));
    echo $outputString; 
    //output will be (test)

strpos () => 문자열에서 첫 번째 발생 위치를 찾는 데 사용됩니다.

strrpos () => 문자열에서 첫 번째 발생 위치를 찾는 데 사용됩니다.


function getStringsBetween($str, $start='[', $end=']', $with_from_to=true){
$arr = [];
$last_pos = 0;
$last_pos = strpos($str, $start, $last_pos);
while ($last_pos !== false) {
    $t = strpos($str, $end, $last_pos);
    $arr[] = ($with_from_to ? $start : '').substr($str, $last_pos + 1, $t - $last_pos - 1).($with_from_to ? $end : '');
    $last_pos = strpos($str, $start, $last_pos+1);
}
return $arr; }

this is a little improvement to the previous answer that will return all patterns in array form:

getStringsBetween('[T]his[] is [test] string [pattern]') will return:


The already posted regex solutions - \((.*?)\) and \(([^\)]+)\) - do not return the innermost strings between an open and close brackets. If a string is Text (abc(xyz 123) they both return a (abc(xyz 123) as a whole match, and not (xyz 123).

The pattern that matches substrings (use with preg_match to fetch the first and preg_match_all to fetch all occurrences) in parentheses without other open and close parentheses in between is, if the match should include parentheses:

\([^()]*\)

Or, you want to get values without parentheses:

\(([^()]*)\)        // get Group 1 values after a successful call to preg_match_all, see code below
\(\K[^()]*(?=\))    // this and the one below get the values without parentheses as whole matches 
(?<=\()[^()]*(?=\)) // less efficient, not recommended

Replace * with + if there must be at least 1 char between ( and ).

Details:

  • \( - an opening round bracket (must be escaped to denote a literal parenthesis as it is used outside a character class)
  • [^()]* - zero or more characters other than ( and ) (note these ( and ) do not have to be escaped inside a character class as inside it, ( and ) cannot be used to specify a grouping and are treated as literal parentheses)
  • \) - a closing round bracket (must be escaped to denote a literal parenthesis as it is used outside a character class).

The \(\K part in an alternative regex matches ( and omits from the match value (with the \K match reset operator). (?<=\() is a positive lookbehind that requires a ( to appear immediately to the left of the current location, but the ( is not added to the match value since lookbehind (lookaround) patterns are not consuming. (?=\() is a positive lookahead that requires a ) char to appear immediately to the right of the current location.

PHP code:

$fullString = 'ignore everything except this (text) and (that (text here))';
if (preg_match_all('~\(([^()]*)\)~', $fullString, $matches)) {
    print_r($matches[0]); // Get whole match values
    print_r($matches[1]); // Get Group 1 values
}

Output:

Array ( [0] => (text)  [1] => (text here) )
Array ( [0] => text    [1] => text here   )

참고URL : https://stackoverflow.com/questions/196520/php-best-way-to-extract-text-within-parenthesis

반응형

'program story' 카테고리의 다른 글

Java의 기본값 및 초기화  (0) 2020.11.04
일반 유형 검사  (0) 2020.11.04
어떤 Lisp를 배워야합니까?  (0) 2020.11.03
mysql에서 선택  (0) 2020.11.03
AngularJS의 ScrollTo 함수  (0) 2020.11.03