PHP의 preg_match(함수)

PHPz
풀어 주다: 2024-08-29 12:49:51
원래의
913명이 탐색했습니다.

PHP 프로그래밍 언어의 preg_match() 함수는 문자열에서 패턴을 검색한 다음 패턴이 존재하는 경우에만 TRUE를 반환하고, 그렇지 않으면 preg_match() 함수는 FALSE를 반환합니다. Preg_match() 함수는 기본적으로 입력 문자열 데이터/문자열 패턴 내부에서 문자열 패턴을 검색하는 데 매우 유용하고 도움이 되는 일부 매개변수를 사용하여 작동합니다. 첫 번째 매개변수는 검색할 문자열 패턴을 저장하는 것입니다. 두 번째 매개변수는 입력 문자열을 저장하는 것입니다. preg_match() 함수를 사용하여 문자열 데이터 내에서 필요한 문자열 패턴을 검색합니다. "일치" 매개변수가 포함되어 인쇄에 사용되면 일치하는 문자열 내용을 찾아 출력에 표시합니다.

광고 이 카테고리에서 인기 있는 강좌 PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

구문:

Int preg_match($pattern, $input, $matches, $flags, $offset)
로그인 후 복사

설명:

  • PHP 언어의 preg_match() 함수는 일반적으로 위의 구문 열에서 언급한 것처럼 preg_match() 함수 내부에 있는 5개의 매개변수만 허용합니다.
  • preg_match() 함수는 패턴, 입력, 일치, 플래그 및 오프셋의 5가지 매개변수로 구성됩니다.
  • preg_match() 함수의 모든 매개변수는 영숫자 문자열 데이터가 포함된 큰 문자열이나 문서 내에서 문자열 패턴을 검색하는 데 유용합니다.

PHP에서 preg_match는 어떻게 작동하나요?

PHP 프로그래밍 언어의 preg_match() 함수는 문자열 문장이나 기타 데이터의 큰 목록 내에서 문자열 패턴을 검색합니다. 문자열 패턴이 발견된 경우에만 TRUE 값을 반환합니다. 그렇지 않으면 FALSE 값을 반환합니다. PHP의 Preg_match() 함수는 일반적으로 preg_match() 함수 내부에 포함된 5개의 매개변수를 기반으로 작동합니다.

아래에 매개변수가 나와 있습니다.

1. 패턴 매개변수: 문자열을 문자열로 검색하기 위해 패턴을 담는 데 사용되는 PHP의 preg_match() 매개변수입니다. 함수 내부에 “$” 기호를 사용하여 패턴을 변수로 표시합니다.

2. 입력 매개변수: preg_match() 함수 내부에 있는 입력 매개변수는 문자열 입력/문자열 입력 값을 보유합니다.

3. Matches 매개변수: preg_match() 내부에 있는 Matches 매개변수는 일치하는 항목이 있는 경우에만 검색 결과를 제공합니다. 일치는 동일한 문자열이 존재하는지 여부를 의미합니다. $matches[0]에는 실제로 전체 일치 문자열 패턴인 전체 텍스트가 포함됩니다. $matches[1]에는 괄호로 묶인 첫 번째 캡처된 하위 패턴 등과 일치하는 문자열 텍스트가 포함됩니다.

대부분 문자열 패턴은 $matches[0][0], $matches[1][0], $matches[2][0], $matches[3][0] 등에서 찾을 수 있습니다. $matches[0][1], $matches[1][1], $matches[2][1] 및 $matches[3][1], 문자열 패턴으로 0을 의미하는 NULL을 찾을 수 있습니다. 일치 배열에 아무 것도 저장하지 않았기 때문에 값을 반환합니다.

4. 플래그 매개변수: 플래그 매개변수에는 문자열 패턴 검색을 처리하는 데 매우 유용한 몇 가지 다른 플래그가 포함될 수 있습니다.

  • PREG_OFFSET_CAPTURE FLAG: preg_match() 함수에 플래그를 전달하면 각 일치 문자열에 대해 추가된 오프셋/오프셋이 반환됩니다.
  • PREG_UNMATCHED_AS_NULL FLAG: 이 플래그는 NULL로 보고하는 데 유용합니다. 플래그가 전달되면 하위 패턴이 전혀 일치하지 않기 때문에 하위 패턴이 NULL로 보고됩니다.

5. 오프셋 매개변수: preg_match() 함수의 오프셋 매개변수는 입력으로 전달되는 문자열의 맨 처음부터 검색하는 데 매우 유용합니다. 이 오프셋 매개변수는 선택 사항이며 항상 필요하지는 않습니다. 요구 사항에 따라 사용할 수 있습니다. "offset" 매개변수는 문자열 검색의 시작 위치를 지정합니다.

6. preg_match()의 반환 값: PHP의 preg_match() 함수는 문자열 패턴이 존재하는 경우에만 항상 TRUE를 반환합니다. 그렇지 않은 경우 preg_match() 함수는 FALSE로 반환합니다.

PHP의 preg_match 예

다음은 예시입니다.

예시 #1

이는 PREG_OFFSET_CAPTURE 플래그를 사용하는 preg_match()를 보여줍니다. “$pavan1”은 문자열 값 “PavanKumarSake”로 생성 및 저장되고 “$pavan1”에 할당됩니다. 그런 다음 preg_match()가 매개변수와 함께 선언됩니다.

‘/(Pavan)(Kumar)(Sake)/’ is the pattern parameter that holds the pattern, which is to search in the input string/string value. Next, we insert the variable “$pavan1” as an input variable. This variable usually contains the string element that we need to search to determine if the string variable’s value exists within it or not. Next, we place the variable “$matches1” after the comma following the variable “$pavan1”. This is helpful to check at what position the patterns are available inside the input string ”PavanKumarSake”.

In the above preg_match() working explanation, we said that what $matches[0] and $matches[1] will return. Likewise, in the below example, $matches[1] will provide the result that is/are fully matched with the pattern/patterns. So the output of array[0] is “PavanKumarSake” because it contains the full string/text.

Then array[1][0] will be “Pavan” then array[2][0] is “Kumar” and array[3][0] is “Sake” based on example1’s output. Then the “print_r($matches1)” is to print what matches are available inside the input string and also shows at what position the string pattern/patterns are available inside the string pattern. Print_r will show the output of the above preg_match() program of PHP Programming Language.

Code:

<?php
$pavan1 = 'PavanKumarSake';
preg_match('/(Pavan)(Kumar)(Sake)/', $pavan1, $matches1, PREG_OFFSET_CAPTURE);
print_r($matches1);
?>
로그인 후 복사

Output:

PHP의 preg_match(함수)

Example #2

In the example below, we assign the string value “www.profitloops.com” to the variable $pro_url. To check if the word “profit” is present in the variable $pro_url, you can use an if statement with the preg_match function. If the function returns a non-zero value, indicating a match, the if condition will consider it as true.

You can then execute the desired statement. The statement “The URL www.profitloops.com contains profit” will be printed if the word “profit” is present in the URL. The browser will display the output below the output section. If the word “profit” is not present in www.profitloops.com, then statements that are in the ELSE condition will be printed.

We use another IF statement to check if the word “loops” appears in the word located at www.profitloops.com. If the condition is a fault, then the ELSE condition’s statements will be printed. But here, the IF conditions are TRUE, so the statements that are inside of the IF will be printed.

Code:

<?php
$pro_url = "www.profitloops.com";
if (preg_match("/profit/", $pro_url))
{
echo "the url $pro_url contains profit , ";
}
else
{
echo "the url $pro_url does not contain profit , ";
}
if (preg_match("/loops/", $pro_url)){
echo "the url $pro_url contains loops , ";
}
else{
echo "the url $pro_url does not contain loops , ";
}
?>
로그인 후 복사

Output:

PHP의 preg_match(함수)

위 내용은 PHP의 preg_match(함수)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!