|
-
Sep 27th, 2005, 07:30 PM
#1
Thread Starter
Hyperactive Member
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.
Last edited by PlaGuE; Sep 27th, 2005 at 08:53 PM.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Sep 27th, 2005, 07:45 PM
#2
Lively Member
Re: separate string into halves?Can i?
You can use explode() which will return an array of the pieces.
Good luck;
Last edited by {yak}; Sep 27th, 2005 at 07:53 PM.
{yak}
-
Sep 27th, 2005, 08:53 PM
#3
Thread Starter
Hyperactive Member
Re: separate string into halves?Can i?
read my changes... lol... i knew i said it wrong
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Sep 28th, 2005, 11:58 AM
#4
Thread Starter
Hyperactive Member
Re: separate string into halves?Can i?
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Sep 28th, 2005, 12:10 PM
#5
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);
-
Sep 29th, 2005, 07:19 PM
#6
Thread Starter
Hyperactive Member
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....
Last edited by PlaGuE; Sep 29th, 2005 at 07:25 PM.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Sep 29th, 2005, 11:22 PM
#7
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
-
Sep 30th, 2005, 12:14 AM
#8
Thread Starter
Hyperactive Member
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
Last edited by PlaGuE; Sep 30th, 2005 at 12:24 AM.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Sep 30th, 2005, 12:20 AM
#9
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.
-
Sep 30th, 2005, 05:57 AM
#10
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);
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 30th, 2005, 08:17 AM
#11
Thread Starter
Hyperactive Member
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.
Last edited by PlaGuE; Sep 30th, 2005 at 01:28 PM.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Sep 30th, 2005, 08:30 AM
#12
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?
-
Sep 30th, 2005, 01:31 PM
#13
Thread Starter
Hyperactive Member
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.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Sep 30th, 2005, 01:46 PM
#14
Re: separate string into halves?Can i?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 30th, 2005, 05:41 PM
#15
Thread Starter
Hyperactive Member
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.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Sep 30th, 2005, 06:41 PM
#16
Re: separate string into halves?Can i?
Can you explain what that does?
-
Sep 30th, 2005, 07:45 PM
#17
Thread Starter
Hyperactive Member
Re: separate string into halves?Can i?
that there is a random string generator.
$string = $key
$key = $items
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Oct 1st, 2005, 04:26 AM
#18
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?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 1st, 2005, 03:05 PM
#19
Thread Starter
Hyperactive Member
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.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Oct 1st, 2005, 03:19 PM
#20
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? 
d34g445gre (10) --> d34-g445g-re
vfj48fjdkie (11) --> vfj4-8fjd-kie
g43cffdeervc (12) --> g43c-ffde-ervc
-
Oct 5th, 2005, 01:34 PM
#21
Thread Starter
Hyperactive Member
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.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Oct 5th, 2005, 02:36 PM
#22
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.
-
Oct 7th, 2005, 08:16 AM
#23
Thread Starter
Hyperactive Member
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....
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Oct 7th, 2005, 08:31 AM
#24
Re: separate string into halves?Can i?
 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.
-
Oct 7th, 2005, 10:38 AM
#25
Thread Starter
Hyperactive Member
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.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Oct 9th, 2005, 11:59 PM
#26
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.
-
Oct 10th, 2005, 03:22 AM
#27
Re: separate string into halves?Can i?
Or you need to come up with a fun formula.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 14th, 2005, 02:56 AM
#28
Thread Starter
Hyperactive Member
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....^_^
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Oct 14th, 2005, 03:29 AM
#29
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 15th, 2005, 07:26 PM
#30
Thread Starter
Hyperactive Member
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]
Last edited by visualAd; Oct 15th, 2005 at 07:48 PM.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|