首页 > 解决方案 > 如何在 [ ] 之间获取字符串

问题描述

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

如何在[]中获取字符串?

标签: phpregex

解决方案


您也可以不使用正则表达式:

$Index1 = strpos($input, '[');
$Index2 = strpos($input, ']');
$Result = substr($input, $Index1+1, $Index2-$Index1-1);

推荐阅读