PHP & Others

preg_match 멀티라인 정규표현식 예제

컨텐츠 정보

본문

m (PCRE_MULTILINE)

s (PCRE_DOTALL)

x (PCRE_EXTENDED)

e (PREG_REPLACE_EVAL)

U (PCRE_UNGREEDY)

 

 

$lineFirst "This is a new first line<br>\r\n";
$lineLast  "This is a new last line<br>\r\n"
;
$page 
= <<<EOD
<html><head>
<title>This is a test page</title>
</head><body>
This is the first line<br>
Hi Fred<br>
Hi Bill<br>
This is the last line<br>
</body>
</html>
EOD;
$re "/<body>.*^(.+)(^.*?^)(.+)(^<\\/body>.*?)/smU"
;
if (
preg_match($re$page$aMatchPREG_OFFSET_CAPTURE
))
$newPage substr($text0$aMatch[1][1
]) .
           
$lineFirst $aMatch[2][0
] .
           
$lineLast $aMatch[4][0
];
print 
$newPage;


http://php.net/manual/en/reference.pcre.pattern.modifiers.php

 

 

관련자료

댓글 0
등록된 댓글이 없습니다.
Today's proverb
인내심을 가져라. 때가 되면 모든 것은 변한다. 조바심이 가을의 영광을 만들거나 살을 에는 겨울을 끝낼 수 는 없다. 근심을 오랫동안 마음에 품고 있으면 그만큼 마음의 평정을 회복하기 어려워진다. 나쁜 생각을 스스로 떨쳐버려라. 나쁜 생각은 영혼의 뿌리를 갉아먹는 악이다.