将内容放在括号中放入数组中

时间:2014-05-13 15:52:40

标签: php arrays string

我想将此字符串放入数组中。每个元素都应该容纳括号的内容。那么有一种简单的方法吗?

(aka "Beep My Dad Says" (2010)) (USA) (alternative title)
 ----------------------------   -----   ----------------
            array[0]            array[1]    array[2]

已尝试类似

的内容
$str = '(aka "Beep My Dad Says" (2010)) (USA) (alternative title)';

if (preg_match("/\(.*?\)/", $str, $matches)) {
    echo $matches[0];
}

5 个答案:

答案 0 :(得分:1)

你只是使它复杂化。以下将会这样做

$arr = explode(') (', trim('(aka "Beep My Dad Says" (2010)) (USA) (alternative title)', '()'));

答案 1 :(得分:1)

Heres怎么做

$string = '(aka "Beep My Dad Says" (2010)) (USA) (alternative title)';

$brackets  = array();

$words = array();
$start = 0;


for($i=0; $i<strlen($string) ; $i++)
{

    if($string[$i]=="(")
    {
      array_push($brackets,"(");


    }
    if($string[$i]==")")
    {
      array_pop($brackets);

    }
    if(count($brackets)==0)
    {

        array_push($words,substr($string,$start+1,$i-$start-1));

    $start = $i+2;
    $i++;
    }

}
print_r($words);

答案 2 :(得分:1)

RegEx正走在正确的道路上。只需添加+修饰符即可贪婪地收集任何尾随的嵌入式括号。

$str = '(aka "Beep My Dad Says" (2010)) (USA) (alternative title)';
/* Regex */
preg_match_all('!\(.*?\)+!',$str,$match);

var_dump($match[0]);
/*
array(3) {
  [0] =>
  string(31) "(aka "Beep My Dad Says" (2010))"
  [1] =>
  string(5) "(USA)"
  [2] =>
  string(19) "(alternative title)"
}
*/

但是这对其他嵌入式引号不起作用,并且不应该信任段之间的空格。原始词法分析器可能是更好的方法。

$str = '(aka "Beep My (Dad) Says" (2010)) (USA)(alternative title)';
/* Lexer/Scanner */
$length = strlen($str);
$stack = array();
$cursor = $nested = 0;
$top = -1;
while ( $cursor < $length ) {
 $c = $str[$cursor++];     // Grab char at index.
 if ( '(' == $c ) {        // Scan for starting character.
   if ( !$nested )         // Check if this is the start of a new segment.
     $stack[++$top] = "";  // Prototype new buffer (i.e. empty string).
  $nested++;               // Increase nesting.
 }
 if ( $nested )
   $stack[$top] .= $c;     // Append character, if inside brackets.
 if ( ')' == $c )          // Scan for ending character.
   $nested--;              // Decrease nesting.
}

var_dump($stack);
/*
array(3) {
  [0] =>
  string(33) "(aka "Beep My (Dad) Says" (2010))"
  [1] =>
  string(5) "(USA)"
  [2] =>
  string(19) "(alternative title)"
}
*/

同样,这只会解决问题,因为包含不均匀括号的字段会混淆任何正则表达式或词法分析器。

$str = '(Sunn O))) "NN O)))" (2000)) (USA) (drone metal)';

理想情况下,您需要返回源生成器,并包含转义(如果可能)。

$str = '(aka "Beep My Dad Says" \(2010\)) (USA) (alternative title)';

答案 3 :(得分:0)

简单是最好的

list($part1, $part2, $part3) = explode(') ', $string);

// Note the space after closing bracket - which is the trick to this

然后,您可以通过ltrim('(')和rtrim(')')运行它们,以便在数组中删除括号(如果不需要)。

答案 4 :(得分:-1)

试试这个,我用你提供的字符串对它进行了测试,无论空格如何,都会提取你需要的内容:

public function stringToArray ( $startingCharacter, $endingCharacter, $string ) {
    $startCounter = 0;
    $strLen = strlen($string);
    $arrayResult = array();

    for ( $i = 0 ; $i < $strLen ; $i++ ) {
        if ( $string[$i] == $startingCharacter ) {
            if ( $startCounter == 0 ) {
                $startingPosition = $i + 1;
            }
            $startCounter++;
        }

        if ( $string[$i] == $endingCharacter && $startCounter == 1 ) {
            $startCounter--;
            $arrayResult[] = substr( $string, $startingPosition , ( $i - $startingPosition ) );
        }
        else if ( $string[$i] == $endingCharacter ) {
            $startCounter--;
        }
    }
}

这里没有函数格式:

$string = '(aka "Beep My Dad Says" (2010))(USA)(alternative title)';

$startingCharacter = '(';
$endingCharacter = ')';
$startCounter = 0;
$strLen = strlen($string);
$arrayResult = array();

for ( $i = 0 ; $i < $strLen ; $i++ ) {
    if ( $string[$i] == $startingCharacter ) {
        if ( $startCounter == 0 ) {
            $startingPosition = $i + 1;
        }
        $startCounter++;
    }

    if ( $string[$i] == $endingCharacter && $startCounter == 1 ) {
        $startCounter--;
        $arrayResult[] = substr( $string, $startingPosition , ( $i - $startingPosition ) );
    }
    else if ( $string[$i] == $endingCharacter ) {
        $startCounter--;
    }
}

希望有所帮助:)