Cheat Sheets & Quick References

PHP String Functions Cheat Sheet

A quick reference to common PHP string functions for searching, trimming, replacing, splitting and safely handling text.

Overview

PHP includes many string functions for everyday text processing. This quick reference groups common operations by task.

Common functions

  • strlen() measures string length in bytes; use multibyte-aware functions when working with UTF-8 characters.
  • trim() removes whitespace from the beginning and end of a string.
  • str_contains() checks whether a string contains another string (PHP 8+).
  • str_starts_with() and str_ends_with() check prefixes and suffixes (PHP 8+).
  • str_replace() replaces occurrences of a search value.
  • explode() splits a string by a delimiter; implode() joins array elements.
  • substr() extracts a byte-oriented substring; consider mb_substr() for multibyte text.

Safety notes

String manipulation is not a substitute for context-aware output encoding. Escape data when rendering it into HTML and validate URL schemes before generating links.

Official resource

PHP String Functions