Regex xml 取出多個詳細的值

來源:互聯網
上載者:User
1A0003|B股指數|BGZS|16
1B0001|工業指數|GYZS|16
1B0002|商業指數|SYZS|16
1B0004|地產指數|DCZS|16
1B0005|公用指數|GYZS|16
1B0006|綜合指數|ZHZS|16

這是xml的部分值,請問要如何依次取出相應的值?
比如將1A0003、1B0001放在一個數組裡,
B股指數、工業指數放在一個數組裡,
BGZS、GYZS放在一個數組。

謝謝各位大牛,
preg_match_all使用三次嗎?
該怎麼寫這樣的運算式?


回複討論(解決方案)

使用正則捕獲功能。

1A0003|B股指數|BGZS|161B0001|工業指數|GYZS|161B0002|商業指數|SYZS|161B0004|地產指數|DCZS|161B0005|公用指數|GYZS|161B0006|綜合指數|ZHZS|16';preg_match_all('/([^|]*)\|([^|]*)\|([^|]*)\|([^<]*)<\/PY>/', $s, $matches);print_r($matches[1]);print_r($matches[2]);print_r($matches[3]);print_r($matches[4]);


結果:
Array
(
[0] => 1A0003
[1] => 1B0001
[2] => 1B0002
[3] => 1B0004
[4] => 1B0005
[5] => 1B0006
)
Array
(
[0] => B股指數
[1] => 工業指數
[2] => 商業指數
[3] => 地產指數
[4] => 公用指數
[5] => 綜合指數
)
Array
(
[0] => BGZS
[1] => GYZS
[2] => SYZS
[3] => DCZS
[4] => GYZS
[5] => ZHZS
)
Array
(
[0] => 16
[1] => 16
[2] => 16
[3] => 16
[4] => 16
[5] => 16
)

$str = <<1A0003|B股指數|BGZS|161B0001|工業指數|GYZS|161B0002|商業指數|SYZS|161B0004|地產指數|DCZS|161B0005|公用指數|GYZS|161B0006|綜合指數|ZHZS|16EOF;$s = simplexml_load_string($str);foreach($s->xpath("//PY") as $k=>$v){list($a[],$b[],$c[])=explode('|',$v);}echo "
";print_r($a);print_r($b);print_r($c);echo "
";/*Array( [0] => 1A0003 [1] => 1B0001 [2] => 1B0002 [3] => 1B0004 [4] => 1B0005 [5] => 1B0006)Array( [0] => B股指數 [1] => 工業指數 [2] => 商業指數 [3] => 地產指數 [4] => 公用指數 [5] => 綜合指數)Array( [0] => BGZS [1] => GYZS [2] => SYZS [3] => DCZS [4] => GYZS [5] => ZHZS)*/

謝謝大牛的方法,兩種方法都試了,都是可以輸出結果的!非常感謝!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.