stacyr Posted February 10, 2008 Share Posted February 10, 2008 hello! anyone know how you convert the string: $currentcategory = "category1_sub1_sub2_sub3" to this: $categorytree = "category1.php : category1_sub1.php : category1_sub1_sub2.php : category1_sub1_sub2_sub3.php" ? i.e. how could you go from "1_2_3" to "1 - 1_2 - 1_2_3" ? Quote Link to comment https://forums.phpfreaks.com/topic/90411-from-1_2_3-to-1-1_2-1_2_3/ Share on other sites More sharing options...
Barand Posted February 10, 2008 Share Posted February 10, 2008 try <?php $str = '1_2_3_4'; $a = explode ('_', $str); $b = array(); for ($i=1, $k=count($a); $i <= $k; $i++) { $b[] = join('_', array_slice($a, 0, $i)); } echo join(' : ', $b); ?> Quote Link to comment https://forums.phpfreaks.com/topic/90411-from-1_2_3-to-1-1_2-1_2_3/#findComment-463494 Share on other sites More sharing options...
sasa Posted February 10, 2008 Share Posted February 10, 2008 try <?php $currentcategory = 'category1_sub1_sub2_sub3'; $currentcategory =explode('_', $currentcategory); for($i = 1; $i < count($currentcategory); $i++) $currentcategory[$i] = $currentcategory[$i - 1].'_'.$currentcategory[$i]; $categorytree =implode('.php : ', $currentcategory).'.php'; echo $categorytree; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90411-from-1_2_3-to-1-1_2-1_2_3/#findComment-463496 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.