PDA

Click to See Complete Forum and Search --> : separate string into halves?Can i?


PlaGuE
Sep 27th, 2005, 07:30 PM
I have a script which generates a a random assortment of numbers and letters.
For use with a signup script.


Now what im trying to do is separate the string and return it with "-"

it could look sumtin like :
randomly generated : 1234123456abcd

Output : 1234-123456-abcd

now i know i prolly could just 3 separate functions... but i dunt wana...

lol.

{yak}
Sep 27th, 2005, 07:45 PM
You can use explode() (http://us3.php.net/explode) which will return an array of the pieces.

Good luck;

PlaGuE
Sep 27th, 2005, 08:53 PM
read my changes... lol... i knew i said it wrong

PlaGuE
Sep 28th, 2005, 11:58 AM
......

visualAd
Sep 28th, 2005, 12:10 PM
Will the string always be in the same format? - If so you can use a regular expression:

$string = '1234123456abcd';

$search = "/^([0-9]{4})([0-9]{6})([a-z]{4})$/i";
$replace = '\1-\2-\3';

$string = preg_replace($search, $replace, $string);

PlaGuE
Sep 29th, 2005, 07:19 PM
koo.. thanks....


the string will be random.

so one instance could be...

1f4ddh-32sa43-1k3d

then another could be

3kd9ff-21k13m-12a3...

so ... how to make it random...hmm....

visualAd
Sep 29th, 2005, 11:22 PM
Regular expressions are basically string templates. So as long as you know what format the string will be in, you can use a regular expression. The above regular expression captures one group of four numeric characters ([0-9]{4}),one group of 6 numeric characters ([0-9]{4}) and one group of 4 alpha characters ([a-z]{4}). To match alpha numeric character you imply add to the character class:

([a-z0-9]{4})

You can find documentation on the syntax regular expressions here:

http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

PlaGuE
Sep 30th, 2005, 12:14 AM
huh...lol....


I dont want it to be (0-9) - (a-z) -(0-9)


i dont understand you...

im trying to get aa random string using 0-9-a-z

I created sumtin that works... but i need it to "SPLIT" the string into peices
ie 1v34-1a0dfo-f4d3

<?
$max = 10; //changable
$items = "1234567890abcdefghijklmnopqrstuvwxyz";
$x = 1;
$total = strlen($items) - 1;

while($x <= $max){

$rand = rand(0, $total);

$item = substr($items, $rand, 1);
if($x == 1){
$key = $item;
}
elseif($x > 1)
{
$key .= $item;
}
$x++;
}
$string = $key;
#############################

$search = "/^([a-z0-9]{4})([a-z0-9]{6})([a-z0-9]{4})/i";
$replace = '\\1-\\2-\\3';

$string = preg_replace($search, $replace, $string);
echo $string;
?>


it splits... but not correctly....

this is sumtin it spit out...
6say-o9qab9-age02
4-6-5

when i want it 4-6-4

visualAd
Sep 30th, 2005, 12:20 AM
What format is the string going to be in? If you want to split the string then you need to have ome kind of consistant format. Once you have decided that you can construct a regular expression to split the string up.

Like I said in my previous post, it doesn't matter if you want a random string. Becuase a regualr expression cna match that - but you do need to know how many characters in each part.

CornedBee
Sep 30th, 2005, 05:57 AM
substr would be faster, you know
$str = substr($str, 0, 4) . '-' . substr($str, 4, 6) . '-' . substr($str, 10);

PlaGuE
Sep 30th, 2005, 08:17 AM
HUH....

@visualAd....

"when i want it '4-6-4'"


@cornbee....

how would i use that. and still make it random.?


~~~~~~~~~~~~~~~~~~
nvm got it....
$string = substr($string, 0,4) . '-' . substr($string, 5,6) . '-' . substr($string, 7, 4);
~~~~~~~~~~`
tho the result is still not what i need.

visualAd
Sep 30th, 2005, 08:30 AM
The contents of the string does not matter. Whether you use CB's method or the Regular Expression method, the characters in the string have no effect.

From what you were saying in your prvious replies it looks like the string length is changing. Is this the case? Could you give us some sample input stirngs and output strings taking into account as many variations as you are expecting?

PlaGuE
Sep 30th, 2005, 01:31 PM
the string length can be changed.

lets say i had:

Randomly created string: 1fj453f25ssk4a

output would then become:

1fj4-53f25s-sk4a

but lets say i made string bigger... then teh ouput would have to change aswell.

CornedBee
Sep 30th, 2005, 01:46 PM
To what?

PlaGuE
Sep 30th, 2005, 05:41 PM
well lets say

$max = 10; //changable



from

$max = 10; //changable
$items = "1234567890abcdefghijklmnopqrstuvwxyz";
$x = 1;
$total = strlen($items) - 1;

while($x <= $max){

$rand = rand(0, $total);

$item = substr($items, $rand, 1);
if($x == 1){
$key = $item;
}
elseif($x > 1)
{
$key .= $item;
}
$x++;
}
$string = $key;



equalled

$max = 15;

instead.

i would need to change the output to make the string even out.

visualAd
Sep 30th, 2005, 06:41 PM
Can you explain what that does?

PlaGuE
Sep 30th, 2005, 07:45 PM
that there is a random string generator.

$string = $key
$key = $items

CornedBee
Oct 1st, 2005, 04:26 AM
But what format should longer strings be in? You have to tell us, else we can do nothing! Do you want to add more fields? Three fields with a certain percentage of the string?

PlaGuE
Oct 1st, 2005, 03:05 PM
okay....


i would like to specify that if the string length is lets say 14

the it would be (4-6-4)

if its 21 : (7-7-7)

tho i would need to be able to change the outcome to anything....

I am having a hard time explaining it...

sorry.

visualAd
Oct 1st, 2005, 03:19 PM
So you would like the string split into three parts with the bigger part in the middle. Is this what you want? Am I understanding this correct? :ehh:

d34g445gre (10) --> d34-g445g-re
vfj48fjdkie (11) --> vfj4-8fjd-kie
g43cffdeervc (12) --> g43c-ffde-ervc

PlaGuE
Oct 5th, 2005, 01:34 PM
well... yeh...

but id also like to have it so that...

i can make split it into however many pieces i need.

visualAd
Oct 5th, 2005, 02:36 PM
You need to be alot more precise about what you want. If you cannot explain give us a few exmaples strings of different lengths before and after the split operation.

PlaGuE
Oct 7th, 2005, 08:16 AM
12k3-3dk4s03k-3dka-d4rfsdfa
12k3-3dk-3d-d4r
j8daf4-f4adfg5-sf43a-l43s-sa4l


I want it so that i can specify how to split the string up.... according to how many chars i got in the string....

visualAd
Oct 7th, 2005, 08:31 AM
12k3-3dk4s03k-3dka-d4rfsdfa
12k3-3dk-3d-d4r
j8daf4-f4adfg5-sf43a-l43s-sa4l


I want it so that i can specify how to split the string up.... according to how many chars i got in the string....
Those exmaples you gave are of no help whatsoever. There is no consistancy in the number of portions and no consistancy in the number of characters for each.

If you want to specify these at runtime you will also need to specify the number of characters in each portion. If that is the case you could write a function which takes an integer for the number of portions and an array for the length of each portion:

<?php
split_string(4, array(3,5,2,6));
?>

You would also need to know the exact length of the string too.

What is this for? - MAybe if you explain that I will understnad better what you are trying to do. :confused:

PlaGuE
Oct 7th, 2005, 10:38 AM
okay...

well...

For my scripts i am wanting to add a subscription key to it...

depending on teh type of lisence they want will determine the length of the string of random characters a-z:0-9


I want to be able to change the length of the separations depending on what its used for and whatever...lol...

Its so that when they get view the key... it goes

1g5ks-3al44k-a3ck9 <-----example

so that when it comes time to input the key...

they get the exact # of chars right per input box...

hopefully that helps a lil more.

visualAd
Oct 9th, 2005, 11:59 PM
What you appear to be asking for isn't possible. You cannot write a function which splits up a string of a random length into a a certain number of parts of certain lenjgths without specifying the size of each part and the number of parts to split it into.

So, what you need to to decide how mandy different formatsd of stirngs you will have and use a function like I suggested in my previous post to split up the string.

CornedBee
Oct 10th, 2005, 03:22 AM
Or you need to come up with a fun formula.

PlaGuE
Oct 14th, 2005, 02:56 AM
acually... i kept implying that i should be able to change teh lengths ... depending on what i need it for.

those were key words....^_^

CornedBee
Oct 14th, 2005, 03:29 AM
But you never gave a method for separating them based on the length. You just made vague posts about separating and length-dependency. Nothing beyond it, and frankly, I'm tired of it.

PlaGuE
Oct 15th, 2005, 07:26 PM
fine... fine fine... ill just do it the way i was gunna do it b4

3 functions....
[REMOVED BY MOD]