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


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

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.