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

 

 

관련자료

등록된 댓글이 없습니다.
Today's proverb
이슬비가 내리고 있다. 당신은 밖에 나가서 우산을 편다. 그것으로 충분하다.“구질구질하게 또 비가 오는군!” 이런 말을 한들 무슨 소용이 있는가. 비도, 구름도, 바람도 결코 마음대로 되지 않는데 어째서 “비 한번 시원스럽게 내리는군”하고 말하지 못하는가. <<날마다 행복해지는 255가지 이야기>> (알랭)