$input="[youtube id=HmV4gXIkP6k]";
preg_match_all("~[(.+?)]~",$input,$output);
var_dump($output);
how to get string inside [ ]?
$input="[youtube id=HmV4gXIkP6k]";
preg_match_all("~[(.+?)]~",$input,$output);
var_dump($output);
how to get string inside [ ]?
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"
}
}
You can also do it without regular expressions:
$Index1 = strpos($input, '[');
$Index2 = strpos($input, ']');
$Result = substr($input, $Index1+1, $Index2-$Index1-1);