Docs/PHP/Regular Expressions

PHP Regular Expressions

Functions

  • preg_match(pattern, subject) — cari 1 match
  • preg_match_all() — cari semua match
  • preg_replace() — search & replace
  • preg_split() — split by pattern
index.php
Try It →
<?php
$text = "Call 081234567890 or 089876543210";

// Match
if (preg_match('/08\d{8,12}/', $text, $match)) {
    echo "First phone: " . $match[0] . "
";
}

// Match all
preg_match_all('/08\d{8,12}/', $text, $matches);
echo "All phones: " . implode(", ", $matches[0]) . "

";

// Validate email
$email = "test@example.com";
if (preg_match('/^[\w.-]+@[\w.-]+\.\w{2,}$/', $email)) {
    echo "✅ Valid email: $email
";
}

// Replace
$censored = preg_replace('/\d/', '*', "Phone: 08123456");
echo "Censored: $censored
";

// Split
$words = preg_split('/[\s,;]+/', "Hello, World; PHP  Regex");
echo "Words: " . implode(" | ", $words);
?>