String Functions


Set the Variables

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


$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 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 whitespace - trim()

Before:

 jdoe 

After:

 jdoe