Like I’m always saying, in today’s data landscape, you are going to process string and text data regularly. PHP has many many string functions built right into the language. Here are 7 of them you may not know about but should…

The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
I’ll cover simple examples of each string function, although real-world use of any of these functions could be more complex and involved.
1. explode()
The explode() function returns an array of strings, having been split on the supplied $separator argument, from the $target_string argument.
The syntax is straightforward:
explode(string $separator, string $target_string);
A good example would be separating out all the segments of a URL using explode():
2. ucwords()
The ucwords() function returns a string with all the words’ first letter capitalized:
ucwords(string $target_string, string $separators = " \t\r\n\f\v");
3. ucfirst()
The ucfirst() function makes a string’s first character capitalized if that character is alphabetical:
ucfirst(string $target_string);
4. strlen()
The strlen() function is one of the easiest on this list of 7. It simply returns the string argument’s length (and counts spaces also).
strlen(string $target_string);
📰 Get your brand, product, or service the attention it deserves with affordable classified ad placement in the OpenLampTech newsletter. I appreciate your support!
5. strstr()
The strstr() function returns the 1st occurrence of a string and can be used in 2 different ways depending on the 3rd argument (if provided in the function call). The syntax is as follows:
strstr(string $haystack, string $needle, bool $before_needle = false);
strstr() option 1: before_needle = true
If the $before_needle argument is TRUE, strstr() returns the string occurrence before the search $needle argument, but does not include the $needle:
strstr() option 2: before_needle = false
However, if the $before_needle argument is FALSE, strstr() returns the string occurrence after the search $needle argument, including the $needle itself:
🗒️ Note: strstr() is case-sensitive.
6. str_replace()
The str_replace() function replaces all occurrences of a particular string with another string. The syntax can seem a little involved but, isn’t too bad honestly:
str_replace(array|string $search, array|string $replace, string|array $subject, int &$count = null);
7. strpos()
The strpos() function returns the numerical position of the first occurrence of a particular substring within the target search string.
strpos(string $target_string, string $search_string, int $offset = 0);
The strpos() function can be used in 3 different ways depending on the 3rd $offset argument if present.
strpos() option 1: offset = 0
If the $offset argument is not present in the strpos() function call, its value is 0 and searching starts at the beginning of $search_string:
strpos() option 2: offset is positive
If the $offset argument is a positive number value in the strpos() function call, searching begins at the beginning of $search_string, offset that many numbers of characters:
strpos() option 3: offset is negative
If the $offset argument is a negative number value in the strpos() function call, searching begins at the end of $search_string, offset that many number of characters:
You can support my blog and work with a donation to my Tip Jar. Thank you so much 🙏
I hope you learned something new about any one of these 7 PHP string functions. Tell me about one of your favorite or most-used PHP string functions in the comments section. Thanks for reading! 📚
Thank you for reading this post. Please share it with someone else who would enjoy it as well.
Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.
Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.
More ways I can help
- Need hosting for your next web application or WordPress site? I highly recommend Hostinger and use them to host my niche bass fishing site. The service is second to none.
- 🔒5 Truths I’ve Come To Realize As a Self-taught Developer
- Desktop and mobile wallpapers, digital downloads, photography services, Shopify and WooCommerce customizations, and content writing – all in one E-commerce Shop. Find your next digital purchase today!
- Take your Laravel applications next level with Battle Ready Laravel by Ash Allen. Learn how to improve the performance, maintainability, and security of your Laravel projects in this e-book.
Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
📰 Get your brand, product, or service the attention it deserves with affordable classified ad placement in the OpenLampTech newsletter. I appreciate your support!