First 4 Letters of a Variable
Posted: Thu Mar 23, 2006 1:04 pm
How can I set var2 to equal the first four characters of var1?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
VKX wrote:How can I set var2 to equal the first four characters of var1?
Code: Select all
$var2 = substr($var1,0,3);This should be working with preg_matchVKX wrote:That's what I was looking for! I was trying preg_match and it wasn't working. Thanks!
Code: Select all
preg_match("%^(.{4})%",$var1,$match);
$var2 = $match[1]; unset($match);