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

how to get string inside [ ]?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

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
  • 317,000
  • 35
  • 244
  • 286
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,704
  • 1
  • 11
  • 14