R
R
Romua1d2015-09-16 12:27:43
PHP
Romua1d, 2015-09-16 12:27:43

preg_match_all skips values, why?

Hello everyone, I wrote a lightweight parser that rips out ip adress and domain from the xml file, which are taken in tags,

$homepage = file_get_contents('url');

preg_match_all("/(\-|((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?))(?=<\/ip>)|([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}(?=<\/dns>)/", $homepage, $output_array);
 foreach ($output_array1[0] as $key => $value) {
        if (($key) % 2 == 0) {
            echo 'add address='. $value .' ';
    }
    elseif (($key + 1) % 2 == 0){

    echo 'name='. $value .'</br>';
    }
 }

The problem is writing to the array, for some reason not all values ​​get into the array. It is in 6-7 values ​​that a mess is observed ... Although the code itself is working in this place. More details on the pictures. The first is an online generator that can handle it with ease and the second is the code in practice.
d47f497a2196406f91267d1a46a8230d.pngfbd87633d55e468c9bb72f998666b58b.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Romua1d, 2015-09-21
@Romua1d

The comments suggested that XML is already the solution to the problem. No need to go around, just take and extract from it those elements that are enclosed in tags

$xml = simplexml_load_file('url');
foreach ($xml->resource as $resource) {
echo "add address=" . $resource->ip . " name=" . $resource->dns . "</br>";
}

O
Optimus, 2015-09-16
Pyan @marrk2

Well, for example, what do you need? regexr.com/3bptl

D
Denis, 2015-09-16
@prototype_denis

... which rips out ip adress and domain from the xml file, which are taken into tags

You are doing this:
<?php

$array = [
    0 => 'Hello',
    1 => 'Word',
];

$string = var_export($array, true);

preg_match_all('/\d+\s=>\s\'(.*?)\'/ismu', $string, $matches);

$hello = $matches[1][0];
$word = $matches[1][1];

var_dump($hello, $word);

Since they are taken in tags, why regular expressions?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question