Page 1 of 1
How do I drop all text after a comma?
Posted: Wed Mar 08, 2006 9:03 am
by webcan
I was wondering if anyone could help me come up with a more efficient way of doing this ...
If $var = "Chicago, IL", and I want $var2 just to be "Chicago", this is what I do now:
Code: Select all
$var = "Chicago, IL";
$vartemp = explode(",", $var, 2);
$var2 = $vartemp[0];
I know there must be an easier way to do this.
Any ideas?
Thanks!
Posted: Wed Mar 08, 2006 9:12 am
by Grim...
From a coding point of view:
Code: Select all
$var = thisFunction("Chicago, IL");
function thisFunction($text)
{
$vartemp = explode(",", $text, 2);
return = $vartemp[0];
}
What I suspect you really want is a
regular expression.
Posted: Wed Mar 08, 2006 9:20 am
by webcan
I suspect I want a regular expression too
I've always been mystified at constructing them. Any suggestion for what the expression for this would be?
(Do you know of any newbie guide to expressions online btw?)
Thanks.
Posted: Wed Mar 08, 2006 9:28 am
by Grim...
You need the crash course:
viewtopic.php?t=33147
There is a specific subforum for regex help:
viewforum.php?f=38
Posted: Wed Mar 08, 2006 9:41 am
by feyd
using
explode() is likely the more effecient way, technically speaking, rather than regex, as initializing the regex engine and running it could easily take more time than required to break the string once on the comma.