---
id: php-regex-functions
title: "PHP RegEx Functions"
category: "Programming_Language"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["preg_match", "preg_replace", "preg_grep", "PHP 정규표현식 함수"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.9
created_at: 2026-07-04
updated_at: 2026-07-04
review_reason: ""
merge_history: []
tags: ["php", "programming", "w3schools", "regex", "functions"]
raw_sources: ["https://www.w3schools.com/php/php_regex_functions.asp"]
applied_in: []
github_commit: ""
---

# [[PHP RegEx Functions]]

## 🎯 한 줄 통찰 (One-line insight)
`preg_match()` and `preg_match_all()` return fundamentally different numbers for the same match — `preg_match()` caps at 1 (match found) or 0 (not found), while `preg_match_all()` returns the actual COUNT of matches, meaning `preg_match()` can never tell you "how many," only "whether." [S1]

## 🧠 핵심 개념 (Core concepts)
- **`preg_match($pattern, $str)`** — returns 1 if found, 0 if not. [S1]
- **`preg_match_all($pattern, $str)`** — returns the total NUMBER of matches. [S1]
- **`preg_replace($pattern, $replacement, $str)`** — replaces all matches with a new string. [S1]
- **`preg_split($pattern, $str)`** — splits a string into an array using regex matches as separators. [S1]
- **`preg_grep($pattern, $array)`** — filters an array, keeping only elements matching the pattern; third argument `PREG_GREP_INVERT` flips this to keep NON-matching elements. [S1]
- **Grouping with `( )`** — applies quantifiers to a whole sub-pattern, and captures sub-matches. [S1]

## 📖 세부 내용 (Details)
- preg_match: `$str = "Visit W3Schools"; $pattern = "/w3schools/i"; echo preg_match($pattern, $str); // 1`. [S1]
- preg_match_all (count): `$str = "The rain in SPAIN falls mainly on the plains."; $pattern = "/ain/i"; echo preg_match_all($pattern, $str); // counts occurrences`. [S1]
- preg_replace: `$str = "Visit Microsoft!"; $pattern = "/microsoft/i"; echo preg_replace($pattern, "W3Schools", $str);`. [S1]
- preg_split: `$str = "This is a text"; $pattern = "/[\s:]/"; $components = preg_split($pattern, $str); print_r($components);`. [S1]
- preg_grep (filter matching): `$input = ["Red", "Pink", "Green", "Blue", "Purple"]; $result = preg_grep("/^p/i", $input); // Pink, Purple`. [S1]
- preg_grep inverted (filter non-matching): `$result = preg_grep("/^p/i", $input, PREG_GREP_INVERT); // Red, Green, Blue`. [S1]
- Grouping: `$str = "Apples and bananas."; $pattern = "/ba(na){2}/i"; echo preg_match($pattern, $str); // matches "banana" pattern: ba + (na)x2`. [S1]

## ⚖️ 모순 및 업데이트 (Contradictions & updates)
- **preg_match와 preg_match_all의 반환값 차이**: 전자는 0/1(존재 여부)만 반환하지만 후자는 실제 매치 개수를 반환한다는 점이 핵심 구분점이다. [S1]

## 🛠️ 적용 사례 (Applied in summary)
현재 발견된 실제 적용 사례가 없습니다 — preg_grep()으로 배열에서 특정 패턴에 맞는 항목만 필터링하는 것이 실전 데이터 필터링의 대표 사례다. [S1]

## 💻 코드 패턴 (Code patterns)
Filtering an array with preg_grep() (PHP):
```php
$input = ["Red", "Pink", "Green", "Blue", "Purple"];
$result = preg_grep("/^p/i", $input); // items starting with "p" (case-insensitive)
print_r($result); // Pink, Purple
```

## ✅ 검증 상태 및 신뢰도
- **상태:** draft
- **검증 단계:** conceptual
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
- **신뢰 점수:** 0.90
- **중복 검사 결과:** 신규 생성 (New discovery)

## 🔗 지식 그래프 (Knowledge Graph)
- **상위/루트:** [[PHP Tutorial]]
- **관련 개념:** [[PHP RegEx]], [[PHP Forms]], [[PHP Form Validation]]
- **참조 맥락:** 정규표현식 함수 활용 — 폼(Forms) 섹션으로 이어짐.

## 📚 출처 (Sources)
- [S1] W3Schools — PHP Regular Expression Functions — https://www.w3schools.com/php/php_regex_functions.asp

## 📝 변경 이력 (Change history)
- 2026-07-04: Initial draft synthesized from the W3Schools "PHP Regular Expression Functions" page (Astra wiki-curation, P-Reinforce v3.1 format).
