Home Quizzes Leaderboard Competitions Learn Hire Us
About Contact
Log In Sign Up
Learn PHP String Functions & Regular Expressions

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
Code Example
<?php

// String basics

$name = " Alice Smith ";

echo strlen($name) . "\n"; // 14 (with spaces)

echo trim($name) . "\n"; // "Alice Smith"

echo strtoupper($name) . "\n"; // " ALICE SMITH "

echo ucwords(trim($name)) . "\n"; // "Alice Smith"

// Searching

$text = "The quick brown fox";

var_dump(str_contains($text, 'fox')); // bool(true) PHP 8+

var_dump(str_starts_with($text, 'The')); // bool(true) PHP 8+

var_dump(str_ends_with($text, 'fox')); // bool(true) PHP 8+

echo strpos($text, 'quick') . "\n"; // 4

// Replace and split

echo str_replace('fox', 'cat', $text) . "\n"; // The quick
brown cat

$words = explode(' ', $text);

echo count($words) . "\n"; // 4

echo implode(' | ', $words) . "\n"; // The | quick | brown
| fox

// Formatting

$price = 1234567.891;

echo number_format($price, 2) . "\n"; // 1,234,567.89

echo sprintf("Name: %s, Age: %d", "Alice", 25) . "\n";

// Regular Expressions

// Validate email

$email = "alice@example.com";

$pattern =
'/^[a-zA-Z0-9.\_%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/';

if (preg_match($pattern, $email)) {

echo "Valid email\n";

} else {

echo "Invalid email\n";

}

// Extract groups

$date = "2024-12-25";

if (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $date, $m)) {

echo "Year: {$m[1]}, Month: {$m[2]}, Day: {$m[3]}\n";

}

// Find all matches

$html = "<a href='link1'>First</a> and <a
href='link2'>Second</a>";

preg_match_all('/href=\'([^']+)\'/', $html, $matches);

print_r($matches[1]); // Array([0] => link1 [1] => link2)

?>
← Inheritance & Interfaces Exception Handling →

Log in to track your progress and earn badges as you complete lessons.

Log In to Track Progress