BudiBadu Logo
Toolbadu

preg_replace Online

Test PHP's preg_replace() function online using real PHP 8.2. Replace strings using regular expressions. Perfect for testing regex patterns and replacements.

preg_replace(string|array $pattern, string|array $replacement, string|array $subject, int $limit = -1, int &$count = null): string|array|null

Pattern (Regex)

Enter your regex pattern (include delimiters like /pattern/)

Replacement

Enter the replacement string (can use $1, $2 for capture groups)

Subject (Text to Replace)

Enter the text where replacements will occur

PHP Code

This is the actual PHP code that will be executed

Loading editor...

Common Replacement Patterns

preg_replace() Function Guide

About preg_replace()

This tool runs actual PHP 8.2 code on our server using Docker containers. The preg_replace() function performs regex-based search and replace operations. It's perfect for complex string transformations, data cleaning, and pattern-based replacements.

  • Replaces all matches by default
  • Supports capture groups ($1, $2, etc.)
  • Can use backreferences in replacement
  • Returns modified string
  • Count parameter tracks replacements

Backreferences & Captures

  • $0 - Full matched text
  • $1, $2, ... - Capture group 1, 2, etc.
  • ${1} - Alternative syntax (clearer)
  • \\1, \\2 - Legacy backreferences

⚠️ Security: Be cautious with user input in patterns. Use preg_quote() to escape special regex characters if needed.

Request a Feature

Have an idea to improve this tool? Share your suggestions and help us make it better! (One request per day)

0/1000 characters