There are a few things you can do to get the first 10 characters from a string in PHP. The easiest way is to use the strlen() function. This function returns the length of the string, in characters. You can also use the substr() function to get the first character from a string. This function takes two arguments: the first argument is the string you want to get the character from, and the second argument is a pointer to another variable that will hold the character that was just extracted from the string. Finally, you can use PHP’s built-in functions strpos() and substr(). These functions will return true if they find a match for their given input, and NULL if they don’t find any matches.


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.