T
T
Timur Kalimullin2018-04-11 16:59:44
PHP
Timur Kalimullin, 2018-04-11 16:59:44

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));

The result should be:
String 1
String 2
<tag1>
Tag str 1
Tag str 2
</tag2>
<text>

Text

Text 2

Text 3


</text>

What is the correct way to add a wrap replacement exception for content between text tags?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2018-04-11
@dimonchik2013

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

F
forspamonly2, 2018-04-11
@forspamonly2

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('')

D
dodo512, 2018-05-20
@dodo512

echo preg_replace('~(\n)[\r\n]+|(<text>.*?</text>)~isu', '$1$2', $content);

https://regex101.com/r/XXttRc/1
https://regex101.com/r/XXttRc/2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question