Jump to content

[SOLVED] Split only first part of a string


techtheatre

Recommended Posts

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

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'];

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.