Regex: find a new line character in a line that is not in textarea

heya, so I'm looking for a regular expression that will allow me to basically replace the newline with any (for example, "xxx"), but only if the newline is not in the textarea tags.

For example, the following:

<strong>abcd
efg</strong>
<textarea>curious
george
</textarea>
<span>happy</span>

It would be:

<strong>abcdxxxefg</strong>xxx<textarea>curious
geroge
</textarea>xxx<span>happy</span>

Does anyone have an idea where should I start? I'm kind of clueless here :( Thanks for any help.

+3
source share
4 answers

I have it, but you won’t like it.;)

$result = preg_replace(
  '~[\r\n]++(?=(?>[^<]++|<(?!/?textarea\b))*+(?!</textarea\b))~',
  'XYZ', $source);

, , , , , <textarea> </textarea>. , , , . </textarea>, , textarea, , .

, ideone. , . , HTML ( minifier); .

  $re=<<<EOT
~
[\r\n]++
(?=
  (?>
    [^<]++            # not left angle brackets, or
  |
    <(?!/?textarea\b) # bracket if not for TA tag (opening or closing)
  )*+
  (?!</textarea\b)    # first TA tag found must be opening, not closing
)
~x
EOT;
+3

, - , , unescape:

<?php //5.3 syntax here

//Regex matches everything within textarea, pre or code tags
$str = preg_replace_callback('#<(?P<tag>textarea|pre|code)[^>]*?>.*</(?P=tag)>#sim',
    function ($matches) { 
         //and then replaces every newline by some escape sequence
         return str_replace("\n", "%ESCAPED_NEWLINE%", $matches[0]);
    }, $str);
//after all we can safely remove newlines
//and then replace escape sequences by newlines
$str = str_replace(array("\n", "%ESCAPED_NEWLINE%"), array('', "\n"), $str);
+1

Why use a regex for this? Why not use a very simple state apparatus for this? Work on a line that searches for opening tags <textarea>, and inside they look for a closing tag. When you come across a new line, convert it or not based on whether you are inside <textarea>or not.

0
source

What you do is parsing HTML. You cannot parse HTML with regular expression.

0
source

Source: https://habr.com/ru/post/1773513/


All Articles