Lets face it, we’re not the best PHP programmers, or else we’d probably remember how to do this without having to look it up all the time, right? I mean, I’ve looked this one up so many times that I’m actually writing about it in the hopes that I’ll remember next time. Sad!
Luckily this one is really easy and our shame will be over quickly. So here’s the syntax:
If you still can’t figure it out, here’s an example:
But if you’re working with strings that could be multi-byte, like unicode strings with special characters, you should probably use mb_substr() instead, which has the same syntax:
You could also use this to get a string out of the middle somewhere by adjusting the start parameter — for example, you could grab the last few characters from the end of the string instead.