一個很簡單的匹配有關問題

來源:互聯網
上載者:User
一個很簡單的匹配問題
-------------文本---------------

Name1 A B C
Name2 A B C
Name3 A B C

-------------文本---------------


A、B、C是3個隨機數字,有時某個數字會為空白(即變為 Name1 A C),請問如何才能在任何情況下取到ABC三個位置的值呢?
謝謝!

------解決方案--------------------
PHP code
$s = <<< TXTName1 A B CName2 A  CName3 A B CTXT;preg_match_all('/([ABC]+)\s+([ABC]*)\s+([ABC]*)\s*$/m', $s, $r);print_r($r);
------解決方案--------------------
嘿嘿...

a.php
-------------文本---------------

Name1 11 23 14
Name2 43 33
Name3 30 44 2

-------------文本---------------
b.php

$file = "a.php"; // 變數賦值
$fp = fopen ( $file, "r" ); // 以唯讀方式開啟檔案
$i = 0; // i=0
while ( ! feof ( $fp ) ) { // while迴圈,條件:沒到檔案的末端。
$buffer = fgets ( $fp, 4096 ); // fgets() 函數用於從檔案中逐行讀取檔案,,讀取的位元組數。預設是 1024 位元組。
preg_replace ( '/ +/', ' ', $buffer );
preg_match_all ( '/ ([0-9]+)/', $buffer, $matches );
if (! empty ( $matches [1] )) {
print_r ( $matches [1] );
}
$i = $i + 1; // 通過while迴圈,帶動i值遞增,實現數組構建。
}
fclose ( $fp ); // 關閉檔案。
?>
------解決方案--------------------
其實還有我覺得還有一個比較大的疑問!因為LZ要的是數字和字母,比如:
文本:
Name1:11 32
我最後取出來的是
PHP code
Array(     [0]=11     [1]=     [2]=32)
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.