Files
2nd/10_Wiki/Dev/Topic_PHP/PHP_RegEx_Functions.md
T
Antigravity Agent 1cfd3bbb56 docs(10_Wiki): 위키 구조 정리 — 언어 튜토리얼 카테고리 폴더 제거 + 신규 자산 동기화
Topic_CSS/Topic_HTML/Topic_JavaScript/Topic_Prompt/Topic_Comfyui 등 기존 카테고리 폴더를 정리하고,
Topic_Graphic/Dev 등 신규 산출물과 Topics 내부 세션/메모리 기록을 동기화.
2026-07-05 00:10:59 +09:00

4.1 KiB

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

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):

$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)

📚 출처 (Sources)

📝 변경 이력 (Change history)

  • 2026-07-04: Initial draft synthesized from the W3Schools "PHP Regular Expression Functions" page (Astra wiki-curation, P-Reinforce v3.1 format).