PHP: Best way to extract text within parenthesis?

后端 未结 8 1429
北海茫月
北海茫月 2020-11-28 06:11

What\'s the best/most efficient way to extract text set between parenthesis? Say I wanted to get the string \"text\" from the string \"ignore everything except this (text)\"

相关标签:
8条回答
  • 2020-11-28 06:46

    So, actually, the code you posted doesn't work: substr()'s parameters are $string, $start and $length, and strpos()'s parameters are $haystack, $needle. Slightly modified:

    $str = "ignore everything except this (text)";
    $start  = strpos($str, '(');
    $end    = strpos($str, ')', $start + 1);
    $length = $end - $start;
    $result = substr($str, $start + 1, $length - 1);

    Some subtleties: I used $start + 1 in the offset parameter in order to help PHP out while doing the strpos() search on the second parenthesis; we increment $start one and reduce $length to exclude the parentheses from the match.

    Also, there's no error checking in this code: you'll want to make sure $start and $end do not === false before performing the substr.

    As for using strpos/substr versus regex; performance-wise, this code will beat a regular expression hands down. It's a little wordier though. I eat and breathe strpos/substr, so I don't mind this too much, but someone else may prefer the compactness of a regex.

    0 讨论(0)
  • 2020-11-28 06:47

    This is a sample code to extract all the text between '[' and ']' and store it 2 separate arrays(ie text inside parentheses in one array and text outside parentheses in another array)

       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);
      }
    

    Output: extract_text("hello how are you?"); will produce:

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

    extract_text("hello [http://www.google.com/test.mp3] how are you?"); will produce

    array(2) {
      [0]=>
      string(6) "hello "
      [1]=>
      string(13) " how are you?"
    }
    
    
    array(1) {
      [0]=>
      string(30) "http://www.google.com/test.mp3"
    }
    
    0 讨论(0)
提交回复
热议问题