Page 1 of 1

How to split a text variable in parts ?

Posted: Mon Oct 21, 2002 11:40 pm
by rax369
I'm doing an application w/PHP and MySQL, and I joined 3 field texts into one... the product name (comes from a database), the price (comes from a database as well), and a 'separator' between them. I joined those in order to show them in a drop down menu, as showed in this image:

Image

To give u the idea of what I did, using the example of the image drop down menu (CELERON 1.7GHZ - $ 117.04), the 1st variable joined was "CELERON 1.7GHZ" the 2nd "- $ " and the 3rd. one "117.04", all this to be able to show them as a whole in the drop down menu.

Now I passed the information of this form to other php page where I need get the variables divided again. What sentence could I use to get the variables chose in the drop down menu divided like this:

1st variable = CELERON 1.7GHZ
2nd variable = - $
3rd variable = 117.04

'cause obviously, the form variable is equal to all that long name "CELERON 1.7GHZ - $ 117.04" (using this example of course)

thx for any solution, for my problem :wink:

Re: How to split a text variable in parts ?

Posted: Mon Oct 21, 2002 11:43 pm
by rax369
rax369 wrote:I'm doing an application w/PHP and MySQL, and I joined 3 field texts into one... the product name (comes from a database), the price (comes from a database as well), and a 'separator' between them. I joined those in order to show them in a drop down menu, as showed in this image:

Image

To give u the idea of what I did, using the example of the image drop down menu (CELERON 1.7GHZ - $ 117.04), the 1st variable joined was "CELERON 1.7GHZ" the 2nd "- $ " and the 3rd. one "117.04", all this to be able to show them as a whole in the drop down menu.

Now I passed the information of this form to other php page where I need get the variables divided again. What sentence could I use :( to get the variables chose in the drop down menu divided like this:

1st variable = CELERON 1.7GHZ
2nd variable = - $
3rd variable = 117.04

'cause obviously, the form variable is equal to all that long name "CELERON 1.7GHZ - $ 117.04"

thx :!: for any solution, to my problem :wink:

Posted: Mon Oct 21, 2002 11:44 pm
by mydimension
explode(" - $ ", "CELERON 1.7GHZ - $ 117.04")
would produce an array with "CELERON 1.7GHZ" as the first elemnt and 117.04 as the second element. :)

Posted: Tue Oct 22, 2002 12:00 am
by rax369
sorry for reply to my message rather than re-edit it (I confused the buttons edit/reply... dont know how :x )
mydimension wrote:explode(" - $ ", "CELERON 1.7GHZ - $ 117.04")
would produce an array with "CELERON 1.7GHZ" as the first elemnt and 117.04 as the second element. :)
so if I assign a variable using 'explode' in order to take out its information, could I do this: :?:

$variable = explode(" - $ ", "CELERON 1.7GHZ - $ 117.04")
echo $variable[0];
echo $variable[1];

Posted: Tue Oct 22, 2002 12:35 am
by rax369
oh sh#@!, that worked perfectly, thanks a lot mydimension, u solved me a problem in which I was thinking a lot about . :!: :!: :!:

I didnt know, tha php had functions like this one 8O

thx man :D

Posted: Tue Oct 22, 2002 3:59 am
by horgh
hehe the power of php is its great variety of functions
try this one: easter_date() lol :wink:

Posted: Tue Oct 22, 2002 2:19 pm
by rax369
ok, one more question about using this useful function called 'explode'.

pretend I have this in a variable:

$JoinedName = INTEL - CELERON 1.7GHZ - $ 117.04

And I wanna split this sentence in 3 different variables like this:
$Brand = INTEL
$NameProd = CELERON 1.7GHZ
$Price = 117.04

Would work, this code to achieve that: :?: :?: :?:

//Splitting the Variables:
$BrandName_Price = explode(" - $ ", $JoinedName);
$Brand_Name = explode(" - ", $BrandName_Price[0]);

//Assigning the Splitted Variables:
$Brand = $Brand_Name[0]; // Equal to = INTEL :?:
$NameProd = $Brand_Name[1]; // Equal to = CELERON 1.7GHZ :?:
$Price = $BrandName_Price[1]; // Equal to = 117.04 :?:

thx for ur rapid answers guys, last time mydimension, really impresed me for the fast answer, after a minute of have been posted my message I got the answer :D ... pretty fast mmm ?

if there is another way to do what I wanna do, show another 'magic' function to do this please, c' ya :wink:

Posted: Tue Oct 22, 2002 2:25 pm
by Zeceer
That looks pretty good to me. Have you tested it?

Posted: Tue Oct 22, 2002 3:14 pm
by rax369
mm.. no, I just asked this here.

I havent tested it, 'cause I wasnt sure it was going to work. :oops:

Posted: Tue Oct 22, 2002 10:08 pm
by mydimension
that should work. it would be even easier if you didn't have that dollar sign in there but oh well. try it out, it looks good from my point of view.

and you are most welcome about the speedy response :wink: