P
P
prostovlad2022-04-14 12:32:51
PHP
prostovlad, 2022-04-14 12:32:51

How to highlight what is between 2 tags?

There is an xml file, it has values

<paramname><![CDATA[Модель]]></paramname>
          <paramvalue><![CDATA[RAK-35REF/RAC-35WEF]]></paramvalue>
          <paramname><![CDATA[Коэффициент SEER / Класс сезонной энергоэффективности (охлаждение)]]></paramname>
          <paramvalue><![CDATA[6,1/A++]]></paramvalue>
          <paramname><![CDATA[Коэффициент/Класс сезонной энергоэффективности (нагрев), SCOP]]></paramname>
          <paramvalue><![CDATA[4,2/A+]]></paramvalue>
          <paramname><![CDATA[Коэффициент/Класс энергоэффективности (охлаждение), EER]]></paramname>
          <paramvalue><![CDATA[3,21/A]]></paramvalue>
          <paramname><![CDATA[Производительность (охлаждение), кВт]]></paramname>
          <paramvalue><![CDATA[3,50 (0,90–4,00)]]></paramvalue>
          <paramname><![CDATA[Потребляемая мощность на охлаждение, кВт]]></paramname>

you need to pull out the values ​​​​between the paramname tags, that is, the model should be pulled out of the line in several ways that I found on the network, for example, these
<paramname><![CDATA[Модель]]></paramname>
preg_match_all('/<paramname[^>]*?>(.*?)<\/paramname>/si',$xml,$matches);
preg_match_all('<paramname>([\s\S]+?)</paramname>',$xml,$matches);

It did not work, as far as I understand the problem is
<![CDATA[ ]]>
how can I solve this issue?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2022-04-14
@prostovlad

preg_match_all('/<paramname>(.+)<\/paramname>/', $text, $matches); // <![CDATA[Модель]]>

preg_match_all('/<paramname><!\[CDATA\[(.+)\]\]><\/paramname>/', $text, $matches); //Модель

Test preg_match_all

N
nokimaro, 2022-04-14
@nokimaro

preg_match_all('~<paramname[^<]*<!\[CDATA\[(.+)\]\]>[^<]*<\/paramname>~', $text, $matches);

And it also makes no sense to parse xml with regular expressions, when you can use the library designed for this.
With your input data, the desired result can be obtained something like this
<?php
$xml = simplexml_load_string($text, null, LIBXML_NOCDATA);

print_r((array)$xml->characteristics->paramvalue);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question