techtheatre Posted May 5, 2007 Share Posted May 5, 2007 I have a variable that contains all the possible options for order status, each separated with the pipe "|" character. The status contains characters and spaces. An example is below. I am trying to ONLY get the first option (everything up to the first pipe character...not including the pipe). I have written the code below as a first attempt, but am sure that there is a MUCH better way to do this. Thanks. $OrderStatusTypes = "Awaiting Confirmation|Processing|Shipped|Cancelled"; $StatusArray = split('[|]', $OrderStatusTypes); $DefaultStatus = $StatusArray[0]; I want the value of $DefaultStatus to be Awaiting Confirmation (though this will change if the variable is changed. Link to comment https://forums.phpfreaks.com/topic/50078-solved-split-only-first-part-of-a-string/ Share on other sites More sharing options...
techtheatre Posted May 5, 2007 Author Share Posted May 5, 2007 I have found a temporary solution, but there is probably a more efficient method...here is what i am using (it works) at the moment...let me know if there is a more elegant way to do this. $OrderStatusTypes = "Awaiting Confirmation|Processing|Shipped|Cancelled"; $StatusArray = explode("|", $OrderStatusTypes); echo "DEFAULT STATUS: ".$StatusArray['0']; Link to comment https://forums.phpfreaks.com/topic/50078-solved-split-only-first-part-of-a-string/#findComment-246205 Share on other sites More sharing options...
effigy Posted May 7, 2007 Share Posted May 7, 2007 Why not use an array? Link to comment https://forums.phpfreaks.com/topic/50078-solved-split-only-first-part-of-a-string/#findComment-247236 Share on other sites More sharing options...
techtheatre Posted May 7, 2007 Author Share Posted May 7, 2007 That is what i have done already...i thought there might be a better way. If not then i will mark this as solved, but i am waiting to see if anyone knows a more direct approach...this method has two parts and ends up with a left-over array that is unused after one line fo code...so it seems that there might be a way to do it without the "scraps"... The array approach certainly works and is what i have implimented in the meantime... If this really is the best option, let me know and i will close this post. Link to comment https://forums.phpfreaks.com/topic/50078-solved-split-only-first-part-of-a-string/#findComment-247289 Share on other sites More sharing options...
effigy Posted May 7, 2007 Share Posted May 7, 2007 An array should be the best fit when working with a list of items. I can't advise further without knowing the bigger picture. Link to comment https://forums.phpfreaks.com/topic/50078-solved-split-only-first-part-of-a-string/#findComment-247318 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.