Jump to content

[SOLVED] strip out . get left/right into variables.


Ninjakreborn

Recommended Posts

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

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

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

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