devnotes.

Common String Functions

strtolower('HELLO');       // 'hello'
strtoupper('hello');       // 'HELLO'
ucfirst('hello world');    // 'Hello world'
ucwords('hello world');    // 'Hello World'
trim('  hello  ');          // 'hello' (removes whitespace both ends)
ltrim(' hello'); rtrim('hello ');

str_replace('world', 'PHP', 'Hello world'); // 'Hello PHP'
substr('Hello world', 0, 5);  // 'Hello'
strpos('Hello world', 'world'); // 6 (position), or false if not found
str_contains('Hello world', 'world'); // true (PHP 8.0+)
str_starts_with('Hello world', 'Hello'); // true (PHP 8.0+)
str_ends_with('Hello world', 'world');   // true (PHP 8.0+)

sprintf('Price: $%.2f', 19.5); // 'Price: $19.50' — formatted output
str_pad('7', 3, '0', STR_PAD_LEFT); // '007'
wordwrap('A long line of text', 10, "\n", true);

Regular expressions (via PCRE) handle pattern matching beyond simple substring checks:

preg_match('/^\d{4}-\d{2}-\d{2}$/', $date);       // check a date format
preg_replace('/[^a-z0-9]/i', '', $input);          // strip non-alphanumeric
preg_match_all('/\b\w+@\w+\.\w+\b/', $text, $matches); // find all emails

Always remember: multi-byte strings (UTF-8, common with non-English text) need the mb_* variants — mb_strlen(), mb_substr(), mb_strtoupper() — because the plain str* functions operate byte-by-byte and can mangle multi-byte characters.

PHP String Functions and Regular Expressions | DevNotes