eludlow Posted September 17, 2008 Share Posted September 17, 2008 I have a string that takes the following form: {1}{2}{3} I need to split the string into an array based on the curly brackets - I think this is best achieved using preg_split() - would that be correct? This would also be one of my first forays into regular expressions - would someone be able to show me the regex needed and explain how it's achieving the split, please? Many thanks in advance, Ed Ludlow Quote Link to comment Share on other sites More sharing options...
effigy Posted September 17, 2008 Share Posted September 17, 2008 <pre> <?php $str = '{1}{2}{3}'; $pieces = preg_split('/[{}]/', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($pieces); ?> </pre> preg_* functions use delimiters, thus the /.../. [...] is a character class which matches one character out of its pool, in this case { or }. You can find more details in my signature links. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 17, 2008 Share Posted September 17, 2008 You can do this: <?php $str = '{1}{2}{3}'; $arr = explode('}{',ltrim(rtrim($str,'}'),'{')); echo '<pre>' . print_r($arr,true) . '</pre>'; ?> No regular expressions needed. Ken Quote Link to comment Share on other sites More sharing options...
eludlow Posted September 17, 2008 Author Share Posted September 17, 2008 Many thanks effigy and Ken - much appreciated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.