Use file data in php code [closed]

1

I would like to know how to modify this code so that it reads the data from the .txt file to search from it.

Code:

<?php

function dijkstra($graph_array, $source, $target) {
    $vertices = array();

    $neighbours = array();
    foreach ($graph_array as $edge) {
        array_push($vertices, $edge[0], $edge[1]);
        $neighbours[$edge[0]][] = array("end" => $edge[1], "cost" => $edge[2]);
        $neighbours[$edge[1]][] = array("end" => $edge[0], "cost" => $edge[2]);
    }
    $vertices = array_unique($vertices);
 echo $vertices[5]."<br>";
    foreach ($vertices as $vertex) {
        $dist[$vertex] = INF;
        $previous[$vertex] = NULL;
    }

    $dist[$source] = 0;
    $Q = $vertices;
    while (count($Q) > 0) {

        // TODO - Find faster way to get minimum
        $min = INF;
        foreach ($Q as $vertex){
            if ($dist[$vertex] < $min) {
                $min = $dist[$vertex];
                $u = $vertex;
                echo $u;
            }
        }

        $Q = array_diff($Q, array($u));
        if ($dist[$u] == INF or $u == $target) {
            break;
        }

        if (isset($neighbours[$u])) {
            foreach ($neighbours[$u] as $arr) {
                $alt = $dist[$u] + $arr["cost"];
                if ($alt < $dist[$arr["end"]]) {
                    $dist[$arr["end"]] = $alt;
                    $previous[$arr["end"]] = $u;
                }
            }
        }
    }
    $path = array();
    $u = $target;
    while (isset($previous[$u])) {
        array_unshift($path, $u);
        $u = $previous[$u];
    }
    array_unshift($path, $u);
    return $path;
}

$graph_array = array(
                    array("a", "b", 7),
                    array("a", "c", 9),
                    array("a", "f", 14),
                    array("b", "c", 10),
                    array("b", "d", 15),
                    array("c", "h", 11),
                    array("c", "f", 2),
                    array("d", "e", 6),
                    array("e", "f", 9),
                    array("f", "h", 7),
                    array("h", "i", 3)
               );

$path = dijkstra($graph_array, "a", "i");

echo "path is: ".implode(", ", $path)."\n";

?>

File txt:

city=A(100,80);
city=B(160,70);
city=C(110,50);
city=D(140,120);
city=F(155,40);
city=G(210,60);
city=H(190,10);
city=I(170,110);
route=A-C;140;
route=A-D;155;
route=C-F;125;
route=D-B;115;
route=D-I;152;
route=B-F;119;
route=B-G;136;
route=G-F;133;
route=F-H;163;
route=I-H;197;
    
asked by anonymous 25.08.2016 / 19:11

1 answer

1

To read the contents of the text file, you can use file_get_contents :

$linhas = file_get_contents('mapa.txt');

To get the data you want, use the preg_match_all method:

preg_match_all("~(?:city|route)=(\w+)(?:\(|\-)(\w+)(?:\,|\;)(\w+)~", $linhas, $pontos);

The variable pontos will contain the results obtained from the expression (?:city|route)=(\w+)(?:\(|\-)(\w+)(?:\,|\;)(\w+) which will correspond to letters and numbers followed by ( or - , letters or numbers followed by , or ; and letters and numbers followed by ) or ; . Try the expression here .

See DEMO

city=A(100,80);
city=B(160,70);
route=A-C;140;
route=A-D;155;

Assuming the file has the above data, the result will be:

  • % A: A, B, A, A ,
  • $pontos[1] : 100, 160, C, D
  • $pontos[2] : 80, 70, 140, 155
26.08.2016 / 18:50