Ninjakreborn Posted March 7, 2007 Share Posted March 7, 2007 I have a string like this 5.Enabled or it could be 5.Disabled It's a number, with a status beside it as shown. If the number was 8 it would be either 8.Enabled 8.Disabled. How can I (in all of these situations), split the id, and the wording on the right. That way I know what number it is in one variable, and whether it's Enabled or Disabled in another variable. I want to trap for instance 8.enabled. I want it take that string and get $number = 8; $status = "Enabled"; basically that type of setup, how would I go about doing that. Link to comment https://forums.phpfreaks.com/topic/41674-solved-strip-out-get-leftright-into-variables/ Share on other sites More sharing options...
suzzane2020 Posted March 7, 2007 Share Posted March 7, 2007 Since u have a structure Number.status ,u cud use the explode function explode(".",$string); the first array element wud be the id and te second element wud be the status Link to comment https://forums.phpfreaks.com/topic/41674-solved-strip-out-get-leftright-into-variables/#findComment-201941 Share on other sites More sharing options...
effigy Posted March 7, 2007 Share Posted March 7, 2007 Is the format always consistent, or do you require more validation? <pre> <?php $tests = array( '5.Enabled', '5.Disabled', '12345.Enabled', 'Enabled.5', '1234.Enabled.1', ); foreach ($tests as $test) { list ($num, $state) = explode('.', $test, 2); printf("Num: %-10s State: %-10s<br>", $num, $state); } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/41674-solved-strip-out-get-leftright-into-variables/#findComment-201945 Share on other sites More sharing options...
Ninjakreborn Posted March 7, 2007 Author Share Posted March 7, 2007 I need a more universal way to get it. it's always the same, I am formatting the information myself from a pre-determined data structure so they are always going to be the same. I am going to need to do it in a foreach Basically I have an array called $id This id array simply has values (no keys that have actually been set). It's a bunch of numbers formatted like that. I am going to need to go through and check all that are enabled, and use them. I need to basically cycle through an array. Say OK id number 3 is disabled so ignore it. number 4 is disabled (ignore it). number 5 is enabled (ok pull it aside, database it properly and move forward), number 6 is disabled (ignore it) number 7 is enabled (ok pull it aside, database it properly and move forward). That is basically the situation I need to get, so right now, I need a way to take the 8.Enabled, and simply get 2 variables (one for the number, one for the status), then do something with it, unset the 2 variables, and repeat the process. Right now I just need to get the 2 variables. Link to comment https://forums.phpfreaks.com/topic/41674-solved-strip-out-get-leftright-into-variables/#findComment-201951 Share on other sites More sharing options...
Ninjakreborn Posted March 7, 2007 Author Share Posted March 7, 2007 Basically part of what you showed would have worked $test = 8.Enabled list ($num, $state) = explode('.', $test, 2); That would basically work, but I have had bad experiences with list, if I can't think of another way, that will work. is there another way, or is that pretty much the only logical way. Link to comment https://forums.phpfreaks.com/topic/41674-solved-strip-out-get-leftright-into-variables/#findComment-201952 Share on other sites More sharing options...
suzzane2020 Posted March 7, 2007 Share Posted March 7, 2007 instead of a list just get the elements into a variable which wud be an array $str=8.Enable; $arr=explode(".",$str); $arr[0] wud be the number $arr[1] wud be the status Link to comment https://forums.phpfreaks.com/topic/41674-solved-strip-out-get-leftright-into-variables/#findComment-201957 Share on other sites More sharing options...
Ninjakreborn Posted March 7, 2007 Author Share Posted March 7, 2007 I could but I try to only use arrays on larger amounts of data. Either way in between the both of you I received enough input to get the fix for what I needed. Thank you both for the feedback. Link to comment https://forums.phpfreaks.com/topic/41674-solved-strip-out-get-leftright-into-variables/#findComment-201958 Share on other sites More sharing options...
boo_lolly Posted March 7, 2007 Share Posted March 7, 2007 split() may be of some use. Link to comment https://forums.phpfreaks.com/topic/41674-solved-strip-out-get-leftright-into-variables/#findComment-202012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.