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
위대한 일을 성취하는 사람은 위대한 고통을 느끼는 사람이다. 고통의 해결은 고통의 감지에서 시작되고, 위대한 고통은 그것을 해결함으로써 인간의 자부심을 드높일 수 있는 위대한 문제이기 때문이다. 따라서 우리는 역설적으로 이렇게 말할 수 있다. 위대한 고통 때문에 아파하는 사람은 축복을 받았다고…. (김광수의 《둥근 사각형의 꿈》중에서)