-1
$input="[youtube id=HmV4gXIkP6k]";
preg_match_all("~[(.+?)]~",$input,$output);
var_dump($output);

how to get string inside [ ]?

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476

2 Answers2

0

You need to escape the square brackets with a backslash, and as a backslash is also an escape character for PHP string literals, you need to escape the backslash also:

$input="[youtube id=HmV4gXIkP6k]";
preg_match_all("~\\[(.+?)\\]~",$input,$output);
var_dump($output);

Output:

array(2) {
  [0]=>array(1) {
    [0]=>string(24) "[youtube id=HmV4gXIkP6k]"
  }
  [1]=>array(1) {
    [0]=>string(22) "youtube id=HmV4gXIkP6k"
  }
}
trincot
  • 263,463
  • 30
  • 215
  • 251
0

You can also do it without regular expressions:

$Index1 = strpos($input, '[');
$Index2 = strpos($input, ']');
$Result = substr($input, $Index1+1, $Index2-$Index1-1);
Przemysław Niemiec
  • 1,521
  • 1
  • 7
  • 13