2017-01-28 84 views
2

我真的不知道有關正則表達式... 所以我卡住了...誰能給我用正則表達式本身的explaintation一個解決方案...正則表達式PHP使preg_split

這裏是我的代碼:

$str = "id:521082299088|name:JOHNSON GREIG DENOIA|mounth:JAN17|amount:170027|admin:2500|billqty:1|metre:R1/900|usage:00010261-00010550|reffno:0BKP21851AF3EC2E0D4F56997EA19DFA|charge:170377|balace:1935"; 
$pregsplit = preg_split("/[\s|]+/",$string2); 

輸出:

Array 
(
    [0] => id:521082299088 
    [1] => name:JOHNSON 
    [2] => GREIG 
    [3] => DENOIA 
    [4] => mounth:JAN17 
    [5] => amount:170027 
    [6] => admin:2500 
    [7] => billqty:1 
    [8] => metre:R1/900 
    [9] => usage:00010261-00010550 
    [10] => reffno:0BKP21851AF3EC2E0D4F56997EA19DFA 
    [11] => charge:170377 
    [12] => balance:1935 
) 

我想這樣的輸出,

Array 
(
    "id" => 521082299088 
    "name" => "JOHNSON GREIG DENOIA" 
    "mount" => "JAN17" 
    "amount" => 170027 
    "admin" => 2500 
    "billqty" => 1 
    "metre" => "R1/900" 
    "usage" => "00010261-00010550" 
    "reffno" => "0BKP21851AF3EC2E0D4F56997EA19DFA" 
    "charge" => 170377 
    "balance" => 1935 
) 

很抱歉的虛設的問題...

回答

4

1)使用preg_match_all功能與特定的正則表達式模式的溶液:

$str = "id:521082299088|name:JOHNSON GREIG DENOIA|mounth:JAN17|amount:170027|admin:2500|billqty:1|metre:R1/900|usage:00010261-00010550|reffno:0BKP21851AF3EC2E0D4F56997EA19DFA|charge:170377|balace:1935"; 

preg_match_all("/(\w+):([^|]+)/", $str, $matches, PREG_SET_ORDER); 
$result = []; 
foreach ($matches as $items) { 
    $result[$items[1]] = $items[2]; 
} 
// $items[1] contains a "parameter" name captured by the first capturing group (\w+) 
// $items[2] contains a "parameter" value captured by the second capturing group ([^|]+) 

print_r($result); 

輸出:

Array 
(
    [id] => 521082299088 
    [name] => JOHNSON GREIG DENOIA 
    [mounth] => JAN17 
    [amount] => 170027 
    [admin] => 2500 
    [billqty] => 1 
    [metre] => R1/900 
    [usage] => 00010261-00010550 
    [reffno] => 0BKP21851AF3EC2E0D4F56997EA19DFA 
    [charge] => 170377 
    [balace] => 1935 
) 

(\w+) - 匹配的所有字母數字字符後跟:

([^|]+) - 匹配的所有字符以外|是定界符

http://php.net/manual/en/function.preg-match-all.php


2)除了第一種方法 - 使用array_combine功能(所有各值從兩個捕獲組結合) :

preg_match_all("/(\w+):([^|]+)/", $str, $matches); 
$result = array_combine($matches[1], $matches[2]); 
// will give the same result 

3)第三種替代方法將使用explode()功能:

$result = []; 
foreach (explode("|", $str) as $items) { 
    $pair = explode(":", $items); 
    $result[$pair[0]] = $pair[1]; 
} 
+0

Thankyouuuuu,幫助我很多。 –

+0

你能解釋一下這個正則表達式嗎?對於每個字符 –

+0

@KurniawanNanda,不客氣 – RomanPerekhrest

0

這種方法將是一種替代方法。

您可以使用PHP的explode()函數將字符串分開並從中創建一個數組。然後,您可以使用strpos()substr()函數分離'key:value'結構。

// input string 
$str = "id:521082299088|name:JOHNSON GREIG DENOIA|mounth:JAN17|amount:170027|admin:2500|billqty:1|metre:R1/900|usage:00010261-00010550|reffno:0BKP21851AF3EC2E0D4F56997EA19DFA|charge:170377|balace:1935"; 

// make an array out of the string, split elements on each pipe character ('|') 
$arr = explode('|', $str); 

// create an output array to keep the results 
$output = []; 

// process the array 
foreach ($arr as $item) { 
    // get delimiter 
    $separatorPos = strpos($item, ':'); 
    // take the key part (The part before the ':') 
    $key = substr($item, 0, $separatorPos); 
    // take the value part (The part after the ':') 
    $value = substr($item, $separatorPos); 
    // push it into the output array 
    $output[$key] = $value; 
} 

// dump the output array 
var_export($output); 

輸出數組的轉儲就像下面這樣:

[ 
    'id'  => ':521082299088', 
    'name' => ':JOHNSON GREIG DENOIA', 
    'mounth' => ':JAN17', 
    'amount' => ':170027', 
    'admin' => ':2500', 
    'billqty' => ':1', 
    'metre' => ':R1/900', 
    'usage' => ':00010261-00010550', 
    'reffno' => ':0BKP21851AF3EC2E0D4F56997EA19DFA', 
    'charge' => ':170377', 
    'balace' => ':1935', 
] 
2

如果無法編寫正expression.Here是使用explode() method.The explode()功能斷線到一個數組一個簡單的解決方案。

<?php 
$str = "id:521082299088|name:JOHNSON GREIG DENOIA|mounth:JAN17|amount:170027|admin:2500|billqty:1|metre:R1/900|usage:00010261-00010550|reffno:0BKP21851AF3EC2E0D4F56997EA19DFA|charge:170377|balace:1935"; 

$array = explode('|',$str); 
foreach($array as $key=>$value){ 
$data = explode(':',$value); 
$final[$data[0]] = $data[1]; 

} 
print_r($final); 

?> 

輸出:

Array 
(
    [id] => 521082299088 
    [name] => JOHNSON GREIG DENOIA 
    [mounth] => JAN17 
    [amount] => 170027 
    [admin] => 2500 
    [billqty] => 1 
    [metre] => R1/900 
    [usage] => 00010261-00010550 
    [reffno] => 0BKP21851AF3EC2E0D4F56997EA19DFA 
    [charge] => 170377 
    [balace] => 1935 
) 

要了解更多關於explode()閱讀文檔http://php.net/manual/en/function.explode.php

+0

感謝您的建議,但我的老師告訴我要學習正則表達式...我也能夠使用爆炸... –

+0

oho然後沒有學習你在這裏發佈post.by祝你好運。 –

1

一種有趣的方式(只有當你的字符串不包含=&):翻譯管道&符號和冒號等號,然後將其解析爲URL查詢parse_str

$str = "id:521082299088|name:JOHNSON GREIG DENOIA|mounth:JAN17|amount:170027|admin:2500|billqty:1|metre:R1/900|usage:00010261-00010550|reffno:0BKP21851AF3EC2E0D4F56997EA19DFA|charge:170377|balace:1935"; 

parse_str(strtr($str, ':|', '=&'), $result); 

print_r($result); 

demo

相關問題