강령은 다음과 같습니다
<?php
$input_lines='<pre class="language-php"><code>m1
</code></pre>
lets go
<pre class="language-php"><code>m2
</code></pre>';
$new_string=preg_replace("/(\r?\n?<pre.*?\/pre>\r?\n?)/s","SAMAN",$input_lines);
echo $new_string;
?>
출력 :
SAMAN lets go SAMAN
정규식 패턴 설명 :
( # Begin capture group
\r?\n? # Optional newline characters on Windows and Linux
<pre.*?\/pre> # Match from opening pre tag to closing pre tag
\r?\n? # Optional newline characters on Windows and Linux
) # End capture group
/s # Force all dots in pattern to allow newline characters
내 대답은 매우 밀접하게 LeleDumbo의 답변을 닮았 아마 묻는 사람의 만족을 수행합니다. 폐쇄 태그에서 불필요한 <
을 생략하고 $ new_string에 숨겨진 문자가 없도록 일부 줄 바꿈 문자가 포함되었습니다 (사용법에 따라 문제가 될 수도 있고 아닐 수도 있습니다).