View Full Version : PHP: How can I set a piece of a string to a variable?


Laogeodritt
04-17-2005, 05:01 AM
Let's say the variable $dairy = "qwerasdf". How would I set the first four digits from the right to another variable ("asdf"), for example? What about the first four digits from the left instead ("qwer")?

Thanks,
Laogeodritt

bigolblob
04-17-2005, 05:15 AM
For the last four digits, it would be:
$var = substr($dairy, -4);

For the first four, it would be:
$var = substr($dairy, 0, 4);

And if you need more information on the substr function, go to: http://php.net/substr

Laogeodritt
04-17-2005, 01:10 PM
Thanks.

There are so many functions, it's difficult to find the one you need if you have no idea what to look for exactly...