7 PHP String Functions You Should Know and How To Use Them

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…

Image by OpenClipart-Vectors from Pixabay 
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():

PHP explode() function

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");
PHP ucwords() function.

3. ucfirst()

The ucfirst() function makes a string’s first character capitalized if that character is alphabetical:

ucfirst(string $target_string);
PHP ucfirst() function

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);
PHP strlen() function.

πŸ“° Get your brand, product, or service the attention it needs with affordable classified ad placement in the OpenLampTech newsletter. Thank you for 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:

PHP strstr() string function with $before_needle TRUE.
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:

PHP strstr() string function with $before_needle FALSE.

πŸ—’οΈ 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);
PHP str_replace() function.

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:

PHP strpos() function with no $offset argument.
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:

PHP strpos() function with a positive $offset argument.
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:

PHP strpos() function with a negative $offset argument.

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! πŸ“š


Like what you have read? See anything incorrect? Please comment below and thank you for reading!!!

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog posts/technical writing I have completed for clients.



To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the β€˜Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

How can I help you?

Disclosure: Some of the services and product links in this post 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 needs with affordable classified ad placement in the OpenLampTech newsletter. Thank you for your support!


Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.