从另一个数组php中搜索字符串数组以进行字符串匹配

时间:2016-07-13 09:41:18

标签: php

我有一个问题,我有两个txt文件,其中一个包含要搜索的内容的值,第二个包含我需要查找包含来自第一个txt文件的信息的文本行的信息。 1. txt文件如下所示:

1461527867
29012520
2220097051
2596180150
29012516
2596180152
29012514

  1. txt文件如下所示:
  2. <?xml version="1.0" encoding="UTF-8"?>
    <osm version="0.6" generator="CGImap 0.4.3 (797 thorn-02.openstreetmap.org)" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
     <bounds minlat="56.7042000" minlon="21.5789000" maxlat="56.7340000" maxlon="21.6328000"/>
     <node id="29012520" visible="true" version="2" changeset="3098893" timestamp="2009-11-12T15:12:59Z" user="Abuls" uid="14113" lat="56.7183050" lon="21.6051939"/>
     <node id="29012518" visible="true" version="2" changeset="3098893" timestamp="2009-11-12T15:12:59Z" user="Abuls" uid="14113" lat="56.7184563" lon="21.6161018"/>
     <node id="29012516" visible="true" version="2" changeset="3100827" timestamp="2009-11-12T19:23:00Z" user="Abuls" uid="14113" lat="56.7180402" lon="21.6222515"/>
     <node id="29012514" visible="true" version="2" changeset="3100827" timestamp="2009-11-12T19:23:00Z" user="Abuls" uid="14113" lat="56.7191410" lon="21.6324709"/>

    所以我需要在2.txt中找到所有行,其中nod id与1.txt文件中给出的nummber相等,而不是我需要删除此搜索字符串行的纬度和经度值。

    有人能帮助我吗?

4 个答案:

答案 0 :(得分:0)

我会给你一些代码让你入门。

// Step 1: Need to load the files:
// put the paths to your files in these two variables
$textFile = 'locations.txt';
$xmlFilePath = 'data.xml';

$idHandle = fopen($textFile, 'r');
$dom = new DOMDocument();
$dom->load($xmlFilePath);
$dom->preserveWhiteSpace = false;

$xpath = new DOMXPath($dom);

// Step 2: getting each line in the text file
while ($id = fgets($idHandle)) {
  $id = (int) $id;
  // Step 3: selecting all the nodes that have ids matching 
  // the id in the text file
  $query = "//node[@id={$id}]";
  $list = $xpath->evaluate($query);

  foreach ($list as $node) {
    echo "Node: {$id}, Latitude: " . $node->getAttribute('lat') 
      . ", " .  "Longitude: " . $node->getAttribute('lon') . "\n"; 
  }
}

我得到以下输出:

Node: 29012520, Latitude: 56.7183050, Longitude: 21.6051939
Node: 29012516, Latitude: 56.7180402, Longitude: 21.6222515
Node: 29012514, Latitude: 56.7191410, Longitude: 21.6324709

如果您还有其他需要,请告诉我。

答案 1 :(得分:0)

您可以结合使用SimpleXMLElementforeachfile_get_contentspreg_split来获得所需的结果。这就是:

    <?php
        // GET THE CONTENTS OF THE TEXT FILE CONTAINING IDS
        // AND BUNDLE IT INTO A VARIABLE FOR LATER USE.
        $txtFileContent = file_get_contents(__DIR__ . "/ids.txt");

        // CONVERT THE CONTENTS OF THE TEXT FILE TO AN ARRAY
        $arrTextData    = preg_split("#[\n\r]#", $txtFileContent);


        // CREATE A SIMPLE XML ELEMENT USING THE XML FILE.
        $sXML           = new SimpleXMLElement(__DIR__ . "/xml_file_name.xml", 0, true);

        // LOOP THROUGH ALL THE ELEMENTS IN THE CHILDREN LIST
        // AND TRY TO MATCH EACH ELEMENT WITH EACH KEY ON THE $arrTextData ARRAY.
        // THIS WOULD IMPLY A NESTED LOOP... ALSO BUILD AN ARRAY OF
        // MATCHED-ELEMENTS WITH THE UNIQUE ID AS THE KEY IN THE PROCESS.
        $arrMatchedElements = array();

        foreach($sXML->children() as $nodeKey=>$nodeData){
            /**@var SimpleXMLElement $nodeData */
            foreach($arrTextData as $iKey=>$ID){
                if($ID == $nodeData->attributes()->id){
                    $arrMatchedElements[$ID]    = $nodeData->attributes();
                }
            }
        }

        var_dump($arrMatchedElements);
        // THE var_dump(...) PRODUCES...            
        array (size=3)
          29012520 => 
            object(SimpleXMLElement)[6]
              public '@attributes' => 
                array (size=9)
                  'id' => string '29012520' (length=8)
                  'visible' => string 'true' (length=4)
                  'version' => string '2' (length=1)
                  'changeset' => string '3098893' (length=7)
                  'timestamp' => string '2009-11-12T15:12:59Z' (length=20)
                  'user' => string 'Abuls' (length=5)
                  'uid' => string '14113' (length=5)
                  'lat' => string '56.7183050' (length=10)
                  'lon' => string '21.6051939' (length=10)
          29012516 => 
            object(SimpleXMLElement)[5]
              public '@attributes' => 
                array (size=9)
                  'id' => string '29012516' (length=8)
                  'visible' => string 'true' (length=4)
                  'version' => string '2' (length=1)
                  'changeset' => string '3100827' (length=7)
                  'timestamp' => string '2009-11-12T19:23:00Z' (length=20)
                  'user' => string 'Abuls' (length=5)
                  'uid' => string '14113' (length=5)
                  'lat' => string '56.7180402' (length=10)
                  'lon' => string '21.6222515' (length=10)
          29012514 => 
            object(SimpleXMLElement)[8]
              public '@attributes' => 
                array (size=9)
                  'id' => string '29012514' (length=8)
                  'visible' => string 'true' (length=4)
                  'version' => string '2' (length=1)
                  'changeset' => string '3100827' (length=7)
                  'timestamp' => string '2009-11-12T19:23:00Z' (length=20)
                  'user' => string 'Abuls' (length=5)
                  'uid' => string '14113' (length=5)
                  'lat' => string '56.7191410' (length=10)
                  'lon' => string '21.6324709' (length=10)

你也可以这样更加面向对象:

    <?php   
        // GET THE CONTENTS OF THE TEXT FILE CONTAINING IDS
        // AND BUNDLE IT INTO A VARIABLE FOR LATER USE. 
        $txtFileContent = file_get_contents(__DIR__ . "/ids.txt");

        // CONVERT THE CONTENTS OF THE TEXT-FILE TO AN ARRAY
        $arrTextData    = preg_split("#[\n\r]#", $txtFileContent);

        // CREATE A SIMPLE XML ELEMENT USING THE XML FILE.
        $sXML           = new SimpleXMLElement(__DIR__ . "/xml_file_name.xml", 0, true);

        // LOOP THROUGH ALL THE ELEMENTS IN THE CHILDREN LIST
        // AND TRY TO MATCH EACH ELEMENT WITH EACH KEY ON THE $arrTextData ARRAY.
        // THIS WOULD IMPLY A NESTED LOOP... ALSO BUILD A MATCHED-ELEMENTS
        // ARRAY IN THE PROCESS.
        $arrMatchedElements = array();

        foreach($sXML->children() as $nodeKey=>$nodeData){
            /**@var SimpleXMLElement $nodeData */
            foreach($arrTextData as $iKey=>$ID){
                if($ID == $nodeData->attributes()->id){
                    $tempPropsObj               = new stdClass();
                    $tempPropsObj->id           = $nodeData->attributes()
                            ->id->__toString();
                    $tempPropsObj->lon          = $nodeData->attributes()->lon->__toString();
                    $tempPropsObj->lat          = $nodeData->attributes()->lon->__toString();
                    $tempPropsObj->uid          = $nodeData->attributes()->uid->__toString();
                    $tempPropsObj->user         = $nodeData->attributes()->user->__toString();
                    $tempPropsObj->timestamp    = $nodeData->attributes()->timestamp->__toString();
                    $tempPropsObj->changeset    = $nodeData->attributes()->changeset->__toString();
                    $tempPropsObj->version      = $nodeData->attributes()->version->__toString();
                    $tempPropsObj->visible      = $nodeData->attributes()->visible->__toString();
                    $arrMatchedElements[$ID]    = $tempPropsObj;
                }
            }
        }

        var_dump($arrMatchedElements);
        // DISPLAYS...
        array (size=3)
          29012520 => 
            object(stdClass)[6]
              public 'id' => string '29012520' (length=8)
              public 'lon' => string '21.6051939' (length=10)
              public 'lat' => string '21.6051939' (length=10)
              public 'uid' => string '14113' (length=5)
              public 'user' => string 'Abuls' (length=5)
              public 'timestamp' => string '2009-11-12T15:12:59Z' (length=20)
              public 'changeset' => string '3098893' (length=7)
              public 'version' => string '2' (length=1)
              public 'visible' => string 'true' (length=4)
          29012516 => 
            object(stdClass)[5]
              public 'id' => string '29012516' (length=8)
              public 'lon' => string '21.6222515' (length=10)
              public 'lat' => string '21.6222515' (length=10)
              public 'uid' => string '14113' (length=5)
              public 'user' => string 'Abuls' (length=5)
              public 'timestamp' => string '2009-11-12T19:23:00Z' (length=20)
              public 'changeset' => string '3100827' (length=7)
              public 'version' => string '2' (length=1)
              public 'visible' => string 'true' (length=4)
          29012514 => 
            object(stdClass)[8]
              public 'id' => string '29012514' (length=8)
              public 'lon' => string '21.6324709' (length=10)
              public 'lat' => string '21.6324709' (length=10)
              public 'uid' => string '14113' (length=5)
              public 'user' => string 'Abuls' (length=5)
              public 'timestamp' => string '2009-11-12T19:23:00Z' (length=20)
              public 'changeset' => string '3100827' (length=7)
              public 'version' => string '2' (length=1)
              public 'visible' => string 'true' (length=4)

希望这有点帮助......好运; - )

答案 2 :(得分:0)

好的,首先在您的示例中,您的XML代码没有结束标记</osm>,这可能只是复制粘贴时的错误。

因为您有一个XML文件,所以您不需要自己解析它,PHP提供了很多功能来为您完成。

据我所知,不可能检查XML的值是否等于另一个值,因此您必须在两个循环中检查它,如下面的示例中所示。

<?php
//Read the id's
$ids = file("txt.txt", FILE_IGNORE_NEW_LINES);

//Read the XML file
$xmlContent = file_get_contents("txt.xml");

//Convert XML file to an object
$xmlObjects = simplexml_load_string($xmlContent);

foreach ($ids as $id) {
    foreach ($xmlObjects->node as $node) {

        //Check if id's are equal
        if ($node["id"] == $id) {
            echo "ID: ".$id;
            echo " Latitude: ".$node["lat"];
            echo " Longidtude ".$node["lon"];
            echo "<br>";
        }
    }
}

输出:

ID: 29012520 Latitude: 56.7183050 Longidtude 21.6051939
ID: 29012516 Latitude: 56.7180402 Longidtude 21.6222515
ID: 29012514 Latitude: 56.7191410 Longidtude 21.6324709

答案 3 :(得分:0)

尝试这样的事情。

$handle = fopen("1.txt", "r");

$xml = simplexml_load_file("2.xml") or die("Error: Cannot create object");

if ($handle) {

    while (($line = fgets($handle)) !== false) {

        foreach ($xml as $key => $value) {

            if ($value['id'] == trim($line)) {

                echo "id: " . $value['id'] . " Lat => " . $value['lat'] . " " . "lon => " . $value['lon'] . "</br>";
            }
        }
    }

    fclose($handle);

} else {
    // error opening the file.
} 

<强>输出

id: 29012520 Lat => 56.7183050 lon => 21.6051939
id: 29012516 Lat => 56.7180402 lon => 21.6222515
id: 29012514 Lat => 56.7191410 lon => 21.6324709