String Functions


Set the variables

In the examples below we have a long string with various cap cases, as well as an example simulationg a user putting space characters into a username input field. We will use these examples to run string functions on them to see how they are parsed.

$test = "the QUICK brown fox jumps over the LAZY dog.";
$username = " jdoe ";

Convert a string to all lower case - strtolower():

the quick brown fox jumps over the lazy dog.

Convert a string to all upper case - strtoupper():

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

Convert a string so that the first word has an initial cap - ucfirst():

The QUICK brown fox jumps over the LAZY dog.

Convert a string to have all initial caps - ucwords():

The QUICK Brown Fox Jumps Over The LAZY Dog.

Find the number count of characters in a string - strlen():

44

Find a string within a string - strstr():

fox jumps over the LAZY dog.

Find and replace a string pattern with a new string - str_replace():

the QUICK brown fox jumps over the LAZY cat.

Remove whitespace (extra spaces) from the beginning or end of a string. Print to screen using pre tags to see the whitespace - trim():

Before:

   jdoe 

After:

jdoe