PHP String Functions

PHP has many predefined string functions that can be used to manipulate strings. The following is a list of string functions provided by PHP. Please refer to php.net for a full list of string functions.

strtolower() Function

The strtolower() function converts all the letter characters of a string to lowercase. The function will take a single string parameter and return a string.

<?php
  $first = "The quick brown fox";
  $second = " jumped over the lazy dog.";

  $third = $first;
  $third .= $second;

  echo strtolower($third);
?>

strtoupper() Function

The strtoupper() function converts all the letter characters of a string to uppercase. The function will take a single string parameter and return a string.

<?php
  $first = "The quick brown fox";
  $second = " jumped over the lazy dog.";

  $third = $first;
  $third .= $second;

  echo strtoupper($third);
?>

ucfirst() Function

The ucfirst() function converts the first character of a string to uppercase. The function will take a single string parameter and return a string.

<?php
  $first = "The quick brown fox";
  $second = " jumped over the lazy dog.";

  $third = $first;
  $third .= $second;

  echo ucfirst($third);
?>

ucwords() Function

The ucwords() function converts the first character of each word in a string to uppercase. The function will take a string parameter and optional delimiters, and it will return a string.

<?php
  $first = "The quick brown fox";
  $second = " jumped over the lazy dog.";

  $third = $first;
  $third .= $second;

  echo ucwords($third);
?>

strlen() Function

The strlen() function returns the length (based on the number of bytes) of a string. The function will return 0 if the string is empty.

NOTE

The strlen() returns the number of bytes NOT the number of characters in a string. While usually the same, they can be different.

<?php
  $first = "The quick brown fox";
  $second = " jumped over the lazy dog.";

  $third = $first;
  $third .= $second;

  echo strlen($third);
?>

trim() Function

The trim() function removes any whitespace from the beginning and end of the provided string. The function will return the trimmed string.

<?php
  echo "A" . trim("  B C D  ") . "E"; 
?>

strstr() Function

The strstr() function finds the first occurrence of a string within a string. The function requires two parameters a haystack (the string to search in) and a needle (the string to search for). The function also can take an optional third parameter, a boolean, with a default value of FALSE.

By default, the strstr() function will return a string from the first occurrence of the needle to the end of the haystack. If the third parameter is set to TRUE, the function will return a string from the being of the haystack to the first occurrence of the needle. The function will return FALSE if the needle is not found.

NOTE

The strstr() function and the strchar() function are the same, as one is an alias of the other.

<?php
  $first = "The quick brown fox";
  $second = " jumped over the lazy dog.";

  $third = $first;
  $third .= $second;

  echo strstr($third, "brown");
?>

str_replace() Function

The str_replace() function replaces all the occurrences of the search string in the subject with the replacement string. The function requires three parameters, a search string, a replacement string, the subject string. An optional fourth parameter can be provided which will limit the number of replacements that can be made.

The function returns a string containing the replacement strings instead of the search string.

<?php
  $first = "The quick brown fox";
  $second = " jumped over the lazy dog.";

  $third = $first;
  $third .= $second;

  echo str_replace("quick", "super-fast", $third);
?>

str_repeat() Function

The str_repeat() function returns the provided string repeated the specified number of times. The function takes two arguments the input string and the number of times to repeat as the multiplier.

<?php
  $first = "The quick brown fox";
  $second = " jumped over the lazy dog.";

  $third = $first;
  $third .= $second;

  echo str_repeat($third, 2);
?>

substr() Function

The substr() function returns the portion of the string that begins with start index and is of the length characters long. The function takes 3 parameters: the string, the start index as a number, and the length as a number.

<?php
  $first = "The quick brown fox";
  $second = " jumped over the lazy dog.";

  $third = $first;
  $third .= $second;

  echo substr($third, 5, 10);
?>

strpos() Function

The strpos() function finds the position of the first occurrence of a substring in a string. The function requires two parameters: the haystack (the string to search in) and a needle (the string to search for). An optional third parameter, offset, can be used to change wherein the string the search should begin.

The strpos() function will return the position (index) of where the needle begins as a number. If the needle is not found, FALSE will be returned.

NOTE

A string first index begins with 0 not 1.

<?php
  $first = "The quick brown fox";
  $second = " jumped over the lazy dog.";

  $third = $first;
  $third .= $second;

  echo strpos($third, "brown");
?>