2017-05-20 22 views
6

我怎樣才能得到一個雙引號的句子,其中有一個點必須拆分?

實例文檔是這樣的:
如何分割文本以匹配雙引號加尾隨文本到點?

「國際象棋可以幫助我們克服困難和痛苦,」 Unnikrishnan說,以我的皇后。 「在棋盤上,你正在戰鬥。因爲我們也在與日常生活中的艱辛搏鬥。「他說。

我想輸出是這樣的:

Array 
(
    [0] =>"Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen. 
    [1] =>"On a chess board you are fighting. as we are also fighting the hardships in our daily life," he said. 
) 

我的代碼仍然用點爆炸。

function sample($string) 
{ 
    $data=array(); 
    $break=explode(".", $string); 
    array_push($data, $break); 

    print_r($data); 
} 

我仍然困惑分體式兩種分隔符關於雙引號和點。因爲在雙引號內有一個包含點分隔符的句子。

回答

0

這裏是使用由preg_split()隨後preg_replace()以固定左和右雙引號了一個更簡單的模式(Demo):

$in='「Chess helps us overcome difficulties and sufferings,」 said Unnikrishnan, taking my queen. 「On a chess board you are fighting. as we are also fighting the hardships in our daily life.」 he said.'; 

$out=preg_split('/ (?=「)/',$in,null,PREG_SPLIT_NO_EMPTY); 

$find='/[「」]/u'; // unicode flag is essential 
$replace='"'; 
$out=preg_replace($find,$replace,$out); // replace curly quotes with standard double quotes 

var_export($out); 

輸出:

array (
    0 => '"Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen.', 
    1 => '"On a chess board you are fighting. as we are also fighting the hardships in our daily life." he said.', 
) 

preg_split()的空間,隨後通過匹配一個(左雙引號)。

0123'步驟需要使用u修飾符的模式以確保識別字符類中的左右雙引號。使用'/「|」/'意味着您可以刪除u修飾符,但它使正則表達式引擎必須執行的步驟加倍(對於這種情況,我的字符類僅使用189步與使用372步的管道字符相比)。

而且關於preg_split()preg_match_all()之間的選擇,去與preg_split()的原因是因爲其目標是僅僅各執後跟一個left double quote空間的字符串。如果目標是省略不與分隔空間字符相鄰的子字符串,則preg_match_all()將是更實際的選擇。

$out=preg_match_all('/「.+?(?= 「|$)/',$in,$out)?$out[0]:null; 
+0

完美的解決方案! – Akintunde007

+0

不錯..但是,我們如何在PHP中打印雙引號? – Rachmad

+0

哦..我知道我的問題,只需編輯.htacces並添加特殊字符'AddDefaultCharset UTF-8 AddCharset UTF-8 .php',謝謝@mickmackusa – Rachmad

2

一個很好的例子爲(*SKIP)(*FAIL)

「[^「」]+」(*SKIP)(*FAIL)|\.\s* 
# looks for strings in double quotes 
# throws them away 
# matches a dot literally, followed by whitespaces eventually 


PHP

$regex = '~「[^「」]+」(*SKIP)(*FAIL)|\.\s*~'; 
$parts = preg_split($regex, $your_string_here); 

這產生

Array 
(
    [0] => 「Chess helps us overcome difficulties and sufferings,」 said Unnikrishnan, taking my queen 
    [1] => 「On a chess board you are fighting. as we are also fighting the hardships in our daily life.」 
) 

a demo on regex101.com以及a demo on ideone.com

+0

真棒..謝謝你的解決方案..這是非常有幫助.. @Jan – Rachmad

+0

你能告訴我什麼字符'的意思〜'在你的正則表達式sintax ? Cz我試圖學習正則表達式,但我沒有在正則表達式中找到字符'〜'。或者你能給我參考一下學習正則表達式的字符嗎?謝謝。 – Rachmad

+0

@Rachmad:這些是分隔符,例如'/'或'#',並且在正則表達式字符串的兩邊都需要。 – Jan

0

或者:

regex10116步

儘管我的邏輯,如果你仍然想使用preg_match_all(),我preg_split()線可以被替換

「.[^」]+」(?:.[^「]+)?

  • 「.[^」]+」匹配之間的一切。
  • (?:.[^「]+)?比賽 - 的可能性,這是爲什麼還有最後? - 一切,這不是一個開始?:意味着非捕獲組。

PHP - PHPfiddle - 點擊 「運行F9」 - [更新替換"]

<?php 
    $str = '「Chess helps us overcome difficulties and sufferings,」 said Unnikrishnan, taking my queen. 「On a chess board you are fighting. as we are also fighting the hardships in our daily life.」'; 

if(preg_match_all('/「.[^」]+」(?:.[^「]+)?/',$str, $matches)){ 
    echo '<pre>'; 
    print_r(preg_replace('[「|」]', '"', $matches[0])); 
    echo '</pre>'; 
} 
?> 

輸出:

Array 
(
    [0] => "Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen. 
    [1] => "On a chess board you are fighting. as we are also fighting the hardships in our daily life." 
)