Page 1 of 1

need some help with explode()

Posted: Sun Aug 28, 2016 4:57 pm
by me!
I have a string that may be formatted in one of the following ways:

XXXXX
XXXXX/Y
Z/XXXXX
Z/XXXXX/Y //not likely but possible

The exact length of characters is never set but the / will always be the separator.
I need to be able to get X and regardless how they enter the data.

If it was just the XXXX/Y I would return the -1 value but with the possibility of having the / in another location I am not sure how to do this. :roll:

Re: need some help with explode()

Posted: Sun Aug 28, 2016 5:40 pm
by requinix
If you, as a human, were presented with each of those four forms, how would you know which one was the XXXXX? explode on "/" and then put that logic you used into code.

Re: need some help with explode()

Posted: Sun Aug 28, 2016 8:36 pm
by me!
Well first ting I would look at is if I have any / at all. Than if I do do I have 1 or 2. From that point I should be able to figure what / I have based on number of letters before and after. But it is a lot of IF statements.

I tried this but it is not working as I expected.

Code: Select all

//$callsign = 'k9dog';
//$callsign = 'k9dog/m';
//$callsign = 'k/k9dog';
$callsign = 'k/k9dog/m';



// fist check to see if we have any "/" in string	
if (preg_match('[/+]', $callsign, $matches)) {
	if (isset($matches[1])){
		echo "Two ".$matches[1]." were found";
		}else{
			 echo "A singel ".$matches[0]." was found";
			}
} else {
    echo "A match was NOT found.";
}

Re: need some help with explode()

Posted: Sun Aug 28, 2016 9:15 pm
by requinix
Which "exact length of characters is never set" are you talking about? Just the Xs? Is the Y and Z always going to be one character?

Try

Code: Select all

// if the string does not have a slash then $parts will contain the full string
$parts = explode("/", $callsign);

if (strlen($parts[0]) > 1) {
	// XXXXX or XXXXX/Y
	$value = $parts[0];
} else {
	// Z/XXXXX or Z/XXXXX/Y
	$value = $parts[1];
}

Re: need some help with explode()

Posted: Sun Aug 28, 2016 9:23 pm
by me!
X will be 3~6 long, is the main call sign
Y can be 1~3 long (optional)
Z is the prefix and can be 1 or 2 long (optional)

X is all that will always be present.

Re: need some help with explode()

Posted: Sun Aug 28, 2016 9:28 pm
by me!
requinix wrote:Which "exact length of characters is never set" are you talking about? Just the Xs? Is the Y and Z always going to be one character?

Try

Code: Select all

// if the string does not have a slash then $parts will contain the full string
$parts = explode("/", $callsign);

if (strlen($parts[0]) > 1) {
	// XXXXX or XXXXX/Y
	$value = $parts[0];
} else {
	// Z/XXXXX or Z/XXXXX/Y
	$value = $parts[1];
}
PERFECT!
So much simpler that what I was trying ! :D

Re: need some help with explode()

Posted: Sun Aug 28, 2016 9:40 pm
by me!
Only thing that would not do was pass just the value is no suffix or prefix was used. So I added this.
Appears to be working well.

Code: Select all

$callsign = 'k9dog';
//$callsign = 'k9dog/m';
//$callsign = 'k/k9dog';
//$callsign = 'k/k9dog/m';


$parts = explode("/", $callsign);
if (isset($parts[0])){
		if (strlen($parts[0]) > 1) {
			// XXXXX or XXXXX/Y
			$value = $parts[0];
		} else {
				// Z/XXXXX or Z/XXXXX/Y
				$value = $parts[1];
		}
	}else{
	$value = $callsign;
	}
	
echo "$value";

Re: need some help with explode()

Posted: Mon Aug 29, 2016 12:40 am
by requinix
me! wrote:X will be 3~6 long, is the main call sign
Y can be 1~3 long (optional)
Z is the prefix and can be 1 or 2 long (optional)
Then what I posted is not sufficient. It assumes that Z will be only one character long, but if it can be two ("kk/k9dog") then the code won't work.

Fortunately there's no way to be ambiguous with those rules:
1. The first part is either 3-6 X (X form) or 1-2 Z (Z/X form)
2. The second part is either 3-6 X (Z/X form) or 1-3 Y (X/Y form)
3. The third part is always 1-3 Y (Z/X/Y form)

and there is no string that will fit into multiple forms:
4. X form is 3-6
5. X/Y form is 3-6/1-3
6. Z/X form is 1-2/3-6
7. Z/X/Y form is 1-2/3-6/1-3

X will always be in the first position (X, X/Y) or second position (Z/X, Z/X/Y).

- If X then [0] will be 3-6 long
- If X/Y then [0] will be 3-6 long
- If Z/X then [0] will be 1-2 long
- If Z/X/Y then [0] will be 1-2 long

So

Code: Select all

if (strlen($parts[0]) >= 3) {
	$value = $parts[0]; // X or X/Y
} else {
	$value = $parts[1]; // Z/X or Z/X/Y
}
which is the same as before but >=3 instead of >1.
me! wrote:Only thing that would not do was pass just the value is no suffix or prefix was used. So I added this.
Appears to be working well.
If $callsign does not contain a slash then $parts will be an array of just $callsign.

explode("/", "k9dog") == array("k9dog")

That isset you have should not be necessary.

Re: need some help with explode()

Posted: Mon Aug 29, 2016 1:33 pm
by Christopher
me! wrote:I have a string that may be formatted in one of the following ways:
XXXXX
XXXXX/Y
Z/XXXXX
Z/XXXXX/Y //not likely but possible


X will be 3~6 long, is the main call sign
Y can be 1~3 long (optional)
Z is the prefix and can be 1 or 2 long (optional)

X is all that will always be present.
Based on these rules, identifying each part can be reduced to rules based on how many items are given. It is possible because of the limited positions and sizes. I expanded the code to make it clear what the rules are:

Code: Select all

$parts = explode('/', $callsign);
switch (count($parts)) {
case 1:                                // only one part, has to be XXXXX
    $x = $part[1];
    $y = '';
    $z = '';
    break;
case 2:                                // can be XXXXX/Y or Z/XXXXX
    if (strlen($part[0]) > 2) {       // if first part too long to be Z then XXXXX/Y
        $x = $part[0];
        $y = $part[1];
        $z = '';
    } else {                           // otherwise has to be Z/XXXXX
        $x = $part[1];
        $y = '';
        $z = $part[0];
    }
    break;
case 3:                                // all three parts, has to be Z/XXXXX/YY
    $x = $part[1];
    $y = $part[2];
    $z = $part[0];
    break;
}