2015-07-06 104 views
2

我有一個像這樣的內容的字符串。PHP:正則表達式,preg_split

19. Which of the following conflicting criteria does the problem below satisfe. 2.1 
C++ pointers are powerful and very flexible but at the cost of poor intelligibility. 
a. Writability vs Readability 
b. Reliability vs Cost of execution 
c. Writability vs Reliability 
d. Cost of execution vs. Readability 
e. Cost of execution vs. Readability 

我想要做的就是像這樣拆分它。

[0] => 19. Which of the following conflicting criteria does the problem below satisfye. 2.1 
C++ pointers are powerful and very flexible but at the cost of poor intelligibility. 

    [1] => a. Writability vs Readability 

    [2] => b. Reliability vs Cost of execution 

    [3] => c. Writability vs Reliability 

    [4] => d. Cost of execution vs. Readability 

    [5] => e. Cost of execution vs. Readability 

我的正則表達式很弱,我有這種結果。

preg_split('/(?=[a-e\d]+\.(?!\d))/', $entries, -1, PREG_SPLIT_NO_EMPTY); 

    [0] => 1 
    [1] => 9. Which of the following conflicting criteria does the problem below satisfy 
    [2] => e. 2.1 
C++ pointers are powerful and very flexible but at the cost of poor intelligibility. 

    [3] => a. Writability vs Readability 

    [4] => b. Reliability vs Cost of execution 

    [5] => c. Writability vs Reliability 

    [6] => d. Cost of execution vs. Readability 

    [7] => e. Cost of execution vs. Readability 

我應該怎麼做?

+1

爲什麼不換行換行符? 「C++指針在自己的路線上強大」? e.'如何生成? – chris85

+0

C++指針功能強大,是第一行的一部分。我不能爆炸它。 – Ian

+0

爲什麼你不能爆炸呢?爆炸在本地測試中工作正常......我仍然沒有看到「e」來自哪裏,但我認爲這是一個錯字? – chris85

回答

1

據我所知,如果有^[a-e\d]+\.ahead(從下面一行開始),您想要在一個或多個\v垂直空格處進行拆分。 preg_split功能是好的:

$pattern = '/\v+(?=^[a-e\d]+\.)/m'; 

m是爲了使^插入符號匹配行開始(不只是字符串開始)的多標誌。

print_r(preg_split($pattern, $str)); 

test at eval.in;應該給出期望的結果:

Array 
(
    [0] => 19. Which of the following conflicting criteria does the problem below satisfe. 2.1 
C++ pointers are powerful and very flexible but at the cost of poor intelligibility. 
    [1] => a. Writability vs Readability 
    [2] => b. Reliability vs Cost of execution 
    [3] => c. Writability vs Reliability 
    [4] => d. Cost of execution vs. Readability 
    [5] => e. Cost of execution vs. Readability 
) 

還有see regex101用於測試分割序列。如果中間有空格的空行,請嘗試使用\s+(一個或多個空格)而不是\v+

+0

我很抱歉我遲到的迴應,這真的幫助了我! – Ian

+0

@Ian偉大的這可以幫助你!ツ也是一個遲到的答案 –

1

如果你只是想打破每行了......

$str = "19. Which of the following conflicting criteria does the problem below satisfe. 2.1 C++ pointers are powerful and very flexible but at the cost of poor intelligibility. 
a. Writability vs Readability 
b. Reliability vs Cost of execution 
c. Writability vs Reliability 
d. Cost of execution vs. Readability 
"; 

preg_match_all('/(.*)\n/', $str, $matches); 

var_dump($matches); 

會給你

array(2) { 
    [0]=> 
    array(5) { 
     [0]=> 
     string(169) "19. Which of the following conflicting criteria does the problem below satisfe. 2.1 C++ pointers are powerful and very flexible but at the cost of poor intelligibility. 
     " 
     [1]=> 
     string(32) "a. Writability vs Readability 
     " 
     [2]=> 
     string(38) "b. Reliability vs Cost of execution 
     " 
     [3]=> 
     string(32) "c. Writability vs Reliability 
     " 
     [4]=> 
     string(39) "d. Cost of execution vs. Readability 
     " 
    } 
    [1]=> 
    array(5) { 
     [0]=> 
     string(168) "19. Which of the following conflicting criteria does the problem below satisfe. 2.1 C++ pointers are powerful and very flexible but at the cost of poor intelligibility." 
     [1]=> 
     string(31) "a. Writability vs Readability" 
     [2]=> 
     string(37) "b. Reliability vs Cost of execution" 
     [3]=> 
     string(31) "c. Writability vs Reliability" 
     [4]=> 
     string(38) "d. Cost of execution vs. Readability" 
    } 
} 

preg_match_all()會給您匹配多行的模式和$matches陣列的能力會給你所有的比賽。由於它會將整行(包括\ n)視爲匹配,所以這是第一個數組。所以你需要(.*)匹配的部分,它位於$matches數組的第二個元素中。

+0

道具爲這個詳細的解釋!謝謝,我會看看這種方法。 – Ian

相關問題