PHP: capture a multiline substring

Questi

I have a long multiline string (its in a file that I can load into a string), lets take this as example:

<response status="success" code="19">
  <result total-count="1" count="1">
    <deviceconfig>
      <system>
        <type>
          <static/>
        </type>
      </system>
      <setting>
    </deviceconfig>
  </result>
</response>
<response status="success" code="19">
  <result total-count="1" count="1">
    <network>
      <interface>
        <ethernet/>
      </interface>
      <profiles>
        <monitor-profile>
          <entry name="default">
            <interval>3</interval>
            <action>wait-recover</action>
          </entry>
        </monitor-profile>
      </profiles>
    </network>
  </result>
</response>

I need to extract the system subset of this and have it in a string with the following content taken from above example:

      <system>
        <type>
          <static/>
        </type>
      </system>

In my use case both the original sample is much much larger as well as the text inside the system is a little bit larger

Questi

ok, i found a solution to do this:

preg_match_all('/(<system>.*<\/system>)/ms', file_get_contents('./myinputfile'), $xmlinit);
$output=$xmlinit[0][0]
file_put_contents('./myoutputfile', $output );

Recommendations for improvements are welcome!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Capture a substring of a matched group

From Dev

capture group with optional substring

From Dev

Multiline capture regex fails if ungreedy

From Dev

Substring to capture one or two digits

From Dev

Can't substring multiline content in an element

From Dev

Get frame of word in a substring in a multiline label

From Dev

How to capture multiline regex between two tags?

From Dev

Capture multiline output as array in bash not working

From Dev

PHP Write to file multiline

From Dev

C# Regex Capture Substring Without Whitespace

From Dev

Regular expression to exclude and capture an optional substring

From Dev

Javascript Regex capture multiline content from each newline entry

From Dev

php cannot work with a substring

From Dev

Substring in php or in sql query

From Dev

Substring js in php

From Dev

Search substring in a PHP Array

From Dev

Capture group + numeral for PHP

From Dev

RegeX in PHP to capture string

From Dev

How to capture an array-formatted substring from a string

From Dev

Why can't I capture more than one digit in substring?

From Dev

php multiline preg_match_all

From Dev

How the expression of multiline string in PHP is better?

From Dev

Display a multiline text properly from MySQL with PHP

From Dev

PHP regex multiline one part of string

From Dev

Embedding multiline alert box in php code

From Dev

php regex to find nested multiline cfml comments

From Dev

Inserting a multiline PHP string into a Javascript variable

From Dev

In PHP substring return �� these type of character?

From Dev

Replace substring in keys of an array in PHP