Page 1 of 1

First 4 Letters of a Variable

Posted: Thu Mar 23, 2006 1:04 pm
by VKX
How can I set var2 to equal the first four characters of var1?

Re: First 4 Letters of a Variable

Posted: Thu Mar 23, 2006 1:10 pm
by jmut
VKX wrote:How can I set var2 to equal the first four characters of var1?

Code: Select all

$var2 = substr($var1,0,3);
Is that what you mean?

Posted: Thu Mar 23, 2006 1:15 pm
by VKX
That's what I was looking for! I was trying preg_match and it wasn't working. Thanks!

Posted: Thu Mar 23, 2006 1:35 pm
by jmut
VKX wrote:That's what I was looking for! I was trying preg_match and it wasn't working. Thanks!
This should be working with preg_match

Code: Select all

preg_match("%^(.{4})%",$var1,$match);
$var2 = $match[1]; unset($match);

Posted: Thu Mar 23, 2006 2:16 pm
by feyd
preg_match() is overkill for this simple of a need.