Answer the question
In order to leave comments, you need to log in
How to replace with a regular expression everywhere except the content between the tags?
Good afternoon, I just can’t think of a regular expression that excludes content between two tags from the search.
There is a file with text in which you need to remove extra line breaks except for the text that is enclosed in tags <text></text>
.
<?
$content = '
String 1
String 2
<tag1>
Tag str 1
Tag str 2
</tag2>
<text>
Text
Text 2
Text 3
</text>
';
echo trim(preg_replace('/[\r\n]+/imu', "\n", $content));
String 1
String 2
<tag1>
Tag str 1
Tag str 2
</tag2>
<text>
Text
Text 2
Text 3
</text>
Answer the question
In order to leave comments, you need to log in
cool boys do it through the DOM tree,
but you can use the "cut-glue" method:
read,
put the texttest into the $THE_TEXT variable , put the randomstring
into the text instead, remove
hyphens in the text,
replace the randomstring with the value of the $THE_TEXT variable,
write it down
and play regexps it's too early for you, but there is an expression, yes
if these text tags are definitely not nested, then you can split the line into pieces according to this tag (opening or closing), then in those pieces that do not start with the opening tag, replace line breaks, and glue everything back together.
sorry, there is no puff at hand, in javascript it will be something like this:
content.split(/(?=<\/?text>)/).map(p => p.match(/^<text>/) ? p : p.replace(/\n\n/g, "\n")).join('')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question