String Functions & Regular Expressions
⏱ 18 min read read
Essential String Functions:
strlen($s) // length
strtoupper($s) // UPPERCASE
strtolower($s) // lowercase
ucfirst($s) // First letter uppercase
ucwords($s) // Every Word Uppercase
trim($s) // remove whitespace from both ends
ltrim($s) / rtrim($s) // left / right trim only
str_pad($s, 10, ' ') // pad to length
str_repeat("abc", 3) // "abcabcabc"
str_replace('old','new',$s) // replace all occurrences
str_contains($s, 'needle') // bool (PHP 8+)
str_starts_with($s, 'pre') // bool (PHP 8+)
str_ends_with($s, 'suf') // bool (PHP 8+)
strpos($s, 'needle') // first position or false
substr($s, 2, 5) // sub-string: start, length
explode(',', $s) // split to array
implode(', ', $arr) // join array to string
sprintf('Name: %s, Age: %d', $name, $age) // format
number_format(1234567.891, 2) // "1,234,567.89"
Regular Expressions:
PHP uses PCRE (Perl-Compatible Regular Expressions) with these
functions:
preg_match('/pattern/', $str) // returns 1 if match, 0 if not
preg_match('/pattern/', $str, $m) // $m[0] = full match,
$m[1] = group 1
preg_match_all('/pattern/', $str, $m) // find ALL matches
preg_replace('/pattern/', 'new', $str) // replace matches
preg_split('/pattern/', $str) // split by regex
PHP regex patterns are enclosed in delimiters: /pattern/flags
Flags: i=case-insensitive, m=multiline, s=dot matches newline
Examples:
/^\d{4}-\d{2}-\d{2}$/ --- matches date like 2024-01-15
/^[a-zA-Z0-9.\_%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/ ---
email
strlen($s) // length
strtoupper($s) // UPPERCASE
strtolower($s) // lowercase
ucfirst($s) // First letter uppercase
ucwords($s) // Every Word Uppercase
trim($s) // remove whitespace from both ends
ltrim($s) / rtrim($s) // left / right trim only
str_pad($s, 10, ' ') // pad to length
str_repeat("abc", 3) // "abcabcabc"
str_replace('old','new',$s) // replace all occurrences
str_contains($s, 'needle') // bool (PHP 8+)
str_starts_with($s, 'pre') // bool (PHP 8+)
str_ends_with($s, 'suf') // bool (PHP 8+)
strpos($s, 'needle') // first position or false
substr($s, 2, 5) // sub-string: start, length
explode(',', $s) // split to array
implode(', ', $arr) // join array to string
sprintf('Name: %s, Age: %d', $name, $age) // format
number_format(1234567.891, 2) // "1,234,567.89"
Regular Expressions:
PHP uses PCRE (Perl-Compatible Regular Expressions) with these
functions:
preg_match('/pattern/', $str) // returns 1 if match, 0 if not
preg_match('/pattern/', $str, $m) // $m[0] = full match,
$m[1] = group 1
preg_match_all('/pattern/', $str, $m) // find ALL matches
preg_replace('/pattern/', 'new', $str) // replace matches
preg_split('/pattern/', $str) // split by regex
PHP regex patterns are enclosed in delimiters: /pattern/flags
Flags: i=case-insensitive, m=multiline, s=dot matches newline
Examples:
/^\d{4}-\d{2}-\d{2}$/ --- matches date like 2024-01-15
/^[a-zA-Z0-9.\_%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/ ---
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress