This is the regular expression you want. It matches tags, even tags, so the php tag ends with the php tag.
/\[(\w+)\](.*?)\[\/\1\]/s
Or, if you want to explicitly match tags that you could use ...
$langs = array('php', 'python', ...); $langs = implode('|', array_map('preg_quote', $langs)); preg_match_all('/\[(' . $langs . ')\](.*?)\[\/\1\]/s', $str, $matches);
source share