如何接收下面提到的类型的数据并提取它

How to receive data of below mentioned type and extract it

我正在尝试插入数据提供者持续提供的值并将其存储到 table 中。下面提到的是格式:

GO XXX,YYY,ZZZ=Ss
            %<QUOTE symbol="XXX" name="testname" exchange="OTHER" basecode="5" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212104600" open="67" high="67" low="64" last="64" previous="59" volume="49000" numtrades="3" pricevolume="322.70" id="combined"/><SESSION day="A" timestamp="20171211000000" last="59" id="previous"/></QUOTE>
            %<QUOTE symbol="YYY" name="testname2" exchange="OTHER" basecode="0" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212102000" open="169" high="169" low="161" last="169" previous="169" volume="60309" numtrades="6" pricevolume="1009.82" id="combined"/><SESSION day="A" timestamp="20171211000000" last="169" id="previous"/></QUOTE>
            %<QUOTE symbol="ZZZ" name="testname3" exchange="OTHER" basecode="9" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212105500" open="8" high="8" low="6" last="6" previous="8" volume="9799179" numtrades="22" pricevolume="7013.11" id="combined"/><SESSION day="A" timestamp="20171211000000" last="8" id="previous"/></QUOTE>

如何将此数据传递给控制器​​函数并提取每个值,例如 'symbol'、'name' 等...?

请帮我解决这个问题...

使用 SimpleXML(删除 % 后 - 假设这是数据的一部分),您可以轻松获得属性...

$data = <<< XML
GO XXX,YYY,ZZZ=Ss
            %<QUOTE symbol="XXX" name="testname" exchange="OTHER" basecode="5" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212104600" open="67" high="67" low="64" last="64" previous="59" volume="49000" numtrades="3" pricevolume="322.70" id="combined"/><SESSION day="A" timestamp="20171211000000" last="59" id="previous"/></QUOTE>
            %<QUOTE symbol="YYY" name="testname2" exchange="OTHER" basecode="0" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212102000" open="169" high="169" low="161" last="169" previous="169" volume="60309" numtrades="6" pricevolume="1009.82" id="combined"/><SESSION day="A" timestamp="20171211000000" last="169" id="previous"/></QUOTE>
            %<QUOTE symbol="ZZZ" name="testname3" exchange="OTHER" basecode="9" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212105500" open="8" high="8" low="6" last="6" previous="8" volume="9799179" numtrades="22" pricevolume="7013.11" id="combined"/><SESSION day="A" timestamp="20171211000000" last="8" id="previous"/></QUOTE>

XML;

preg_match_all("/%<QUOTE(.*?)<\/QUOTE>/", $data, $list);
foreach ( $list[0] as $item )  {
    $item = ltrim($item, "%");
    $xml = simplexml_load_string($item);
    echo (string)$xml['exchange'].PHP_EOL;
    foreach ( $xml->SESSION as $session )   {
        echo $session['day'].'-'.$session['timestamp'].PHP_EOL;
    }
}

以上代码输出...

OTHER
B-20171212104600
A-20171211000000
OTHER
B-20171212102000
A-20171211000000
OTHER
B-20171212105500
A-20171211000000

我已经用您的新数据结构更新了代码,我正在使用正则表达式——我不是很喜欢它,但这应该只提取您需要的组件。一旦它像以前一样提取每个单独的项目,它就会使用相同的代码结构。

要将数据插入数据库,您需要了解 PDO 之类的内容以及如何将数据添加到数据库(https://phpdelusions.net/pdo#dml 之类的内容会有帮助)。

您好,请查看 simpleXML 的官方 PHP 文档:http://php.net/manual/en/ref.simplexml.php

我创建了一个小片段,应该可以帮助您入门:https://eval.in/918963

$string = '<QUOTE symbol="XXX" name="name_xxx" exchange="testdata" basecode="C" pointvalue="1.0" tickincrement="1" ddfexchange="U" lastupdate="20171212095542" mode="R">
<SESSION day="B" timestamp="20171212104600" open="67" high="67" low="64" last="64" previous="59" volume="49000" numtrades="3" pricevolume="0000" id="combined"/>
<SESSION day="A" timestamp="20171211000000" last="59" id="previous"/>
</QUOTE>';

$xml = simplexml_load_string($string);

//Get all QUOTE attributes
print_r($xml->attributes());

//Get the first SESSION
print_r($xml->SESSION[0]);

//Get all attributes of SESSION[0]
print_r($xml->SESSION[0]->attributes());

//Get a specific attribute of SESSION[0]
print_r($xml->SESSION[0]->attributes()->day);