Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How to do this in $string:
$string
preg_replace("~\r\n~i","",$string)
But not between <script> </script>
<script> </script>
诀窍是将<script>标签放入子模式并再次插入。通过这种方式,我们可以保护它不被触摸。
<script>
preg_replace('~\r\n|(<script>.*?</script>)~s', '$1', $str);
~s也需要.匹配换行符的修饰符。
~s
.
简化模式的学分来自评论中的@m.buettner。