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
지금이 바로 새 출발점. 인생이란 하루하루가 훈련이다. 우리 자신을 훈련하는 터전이다. 실패도 할 수 있는 훈련장이다. 살아있음이 흥겨운 훈련장이다. 지금 이 행복을 기뻐하지 않고 언제 어디서 행복해지랴. 이 기쁨을 발판삼아 온 힘으로 나아가자. 나의 미래는 지금 이 순간 이 곳에 있다. 지금 여기서 노력하지 않고, 언제 어디서 노력하랴. (오히라 미쓰요의 <그러니까 당신도 살아> 중에서)