How to do this in $string
:
preg_replace("~\r\n~i","",$string)
But not between <script> </script>
How to do this in $string
:
preg_replace("~\r\n~i","",$string)
But not between <script> </script>
The trick is to take the <script>
tag into a subpattern and insert it again. In this way we safe it from being touched.
preg_replace('~\r\n|(<script>.*?</script>)~s', '$1', $str);
The ~s
modifier is needed that .
matches newlines too.
Credits for the simplified pattern goes to @m.buettner from the comments.