How to split a text variable in parts ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

How to split a text variable in parts ?

Post 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:
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Re: How to split a text variable in parts ?

Post 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:
Last edited by rax369 on Mon Oct 21, 2002 11:44 pm, edited 1 time in total.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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. :)
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post 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];
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post 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
User avatar
horgh
Forum Newbie
Posts: 23
Joined: Mon Oct 21, 2002 9:50 am
Location: GER
Contact:

Post by horgh »

hehe the power of php is its great variety of functions
try this one: easter_date() lol :wink:
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post 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:
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

That looks pretty good to me. Have you tested it?
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

mm.. no, I just asked this here.

I havent tested it, 'cause I wasnt sure it was going to work. :oops:
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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:
Post Reply