-
separate string into halves?Can i?
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.
-
Re: separate string into halves?Can i?
You can use explode() which will return an array of the pieces.
Good luck;
-
Re: separate string into halves?Can i?
read my changes... lol... i knew i said it wrong
-
Re: separate string into halves?Can i?
-
Re: separate string into halves?Can i?
Will the string always be in the same format? - If so you can use a regular expression:
PHP Code:
$string = '1234123456abcd';
$search = "/^([0-9]{4})([0-9]{6})([a-z]{4})$/i";
$replace = '\1-\2-\3';
$string = preg_replace($search, $replace, $string);
-
Re: separate string into halves?Can i?
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....
-
Re: separate string into halves?Can i?
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/referen...ern.syntax.php
-
Re: separate string into halves?Can i?
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
Code:
<?
$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
-
Re: separate string into halves?Can i?
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.
-
Re: separate string into halves?Can i?
substr would be faster, you know
Code:
$str = substr($str, 0, 4) . '-' . substr($str, 4, 6) . '-' . substr($str, 10);
-
Re: separate string into halves?Can i?
HUH....
@visualAd....
"when i want it '4-6-4'"
@cornbee....
how would i use that. and still make it random.?
~~~~~~~~~~~~~~~~~~
nvm got it....
PHP Code:
$string = substr($string, 0,4) . '-' . substr($string, 5,6) . '-' . substr($string, 7, 4);
~~~~~~~~~~`
tho the result is still not what i need.
-
Re: separate string into halves?Can i?
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?
-
Re: separate string into halves?Can i?
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.
-
Re: separate string into halves?Can i?
-
Re: separate string into halves?Can i?
well lets say
PHP Code:
$max = 10; //changable
from
PHP Code:
$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
instead.
i would need to change the output to make the string even out.
-
Re: separate string into halves?Can i?
Can you explain what that does?
-
Re: separate string into halves?Can i?
that there is a random string generator.
$string = $key
$key = $items
-
Re: separate string into halves?Can i?
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?
-
Re: separate string into halves?Can i?
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.
-
Re: separate string into halves?Can i?
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
-
Re: separate string into halves?Can i?
well... yeh...
but id also like to have it so that...
i can make split it into however many pieces i need.
-
Re: separate string into halves?Can i?
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.
-
Re: separate string into halves?Can i?
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....
-
Re: separate string into halves?Can i?
Quote:
Originally Posted by PlaGuE
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 Code:
<?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:
-
Re: separate string into halves?Can i?
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.
-
Re: separate string into halves?Can i?
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.
-
Re: separate string into halves?Can i?
Or you need to come up with a fun formula.
-
Re: separate string into halves?Can i?
acually... i kept implying that i should be able to change teh lengths ... depending on what i need it for.
those were key words....^_^
-
Re: separate string into halves?Can i?
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.
-
Re: separate string into halves?Can i?
fine... fine fine... ill just do it the way i was gunna do it b4
3 functions....
[REMOVED BY MOD]