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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.