Hood15 Posted May 13, 2008 Share Posted May 13, 2008 Hello, I've already done the task by using explode and stuff. However, I think that regex would be faster and the right thing to use. Could anyone show me how this can be done with regex? I'm attempting to change l:blocks/nav.tpl;r:blocks/your_account.tpl,blocks/whos_online.tpl in to Array ( [l] => Array ( [0] => blocks/nav.tpl ) [r] => Array ( [0] => blocks/your_account.tpl [1] => blocks/whos_online.tpl ) ) The code I used to get that with explode was $_blocks = explode(';', $main_class->modules->info['blocks']); for ($i = 0; $i < count($_blocks); $i++) { $side = explode(':', $_blocks[$i]); $files = explode(',', $side[1]); for ($file_i = 0; $file_i < count($files); $file_i++) { $blocks["{$side[0]}"][] = $files["{$file_i}"]; } } print_r($blocks); Link to comment https://forums.phpfreaks.com/topic/105382-regex-help/ Share on other sites More sharing options...
effigy Posted May 13, 2008 Share Posted May 13, 2008 Not a vast improvement, but regex nonetheless: <pre> <?php $str = 'l:blocks/nav.tpl;r:blocks/your_account.tpl,blocks/whos_online.tpl'; preg_match_all('/([lr])[^;]+)/', $str, $matches); $sides = count($matches[0]); for ($i = 0; $i < $sides; $i++) { $blocks[$matches[1][$i]] = explode(',', $matches[2][$i]); } print_r($blocks); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/105382-regex-help/#findComment-539898 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.