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 Link to comment https://forums.phpfreaks.com/topic/124641-solved-help-with-string-splitting/ 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. Link to comment https://forums.phpfreaks.com/topic/124641-solved-help-with-string-splitting/#findComment-643727 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 Link to comment https://forums.phpfreaks.com/topic/124641-solved-help-with-string-splitting/#findComment-643728 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. Link to comment https://forums.phpfreaks.com/topic/124641-solved-help-with-string-splitting/#findComment-643732 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.