Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php string separation

Status
Not open for further replies.

electricphp

Programmer
Feb 20, 2008
71
US
If I have a $_post variable that holds the value "red - 45"

and I would like to load that into two different variables:

$color and $price, how would I go about it?
 
try
Code:
$a = explode(' - ',$_POST['string']);

This will give you an array containing your values

$a[0] = red
$a[1] = 45

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
You can also use

list($color, $price)=split(" - ", $_POST['varname']);

echo $color = red
echo $price = 45

Good luck!
 
just for interest split is not a synonym for explode, although they have similar results. split engages the regular expression engine in php, which is inefficient (it's looking better in php 6). explode does not, and for simple tasks, should be favoured.

the manual also favours preg_split() over split.

php manual said:
Tip
preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split(). If you don't require the power of regular expressions, it is faster to use explode()

 
Can I pretend that I knew that? :)

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
I knew that.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top