2013-02-20 38 views
-1

我有一個字符串象下面這樣:如何獲得價值高達定字符串的一定長度在PHP

例如:

$string = "A great way to start with your theme is to use the XML export file from the demo. Having this starting content makes it easier to see how some features are created. Download the demo content below and import it into your WordPress site to get a replica of the theme demo site.The second step is to navigate to the Tools options and click Import. Then on the bottom of the list."; 

現在我需要得到含量高達3行,它應該是一個完整的句子。所以我想知道我該怎麼做。 PHP中有任何功能來做到這一點?

+0

你的意思是第三個字符?所有這些文本都在一行中,我看不到新的換行符\ n。 – 2013-02-20 10:11:45

+4

您的輸入中只有一行?另外,你如何認識一句話 - 如果在一個句子裏面有一個'.'如果是這樣的話:'我今年賺到了$ 1,000,000,000.00!' – 2013-02-20 10:11:47

+1

你的意思是3行意味着3 fullstop? – 2013-02-20 10:13:19

回答

0

這應該您開始使用一些基本的正則表達式。當遇到異常情況時,您應該不斷重新測試正則表達式的有效性並對其進行調整。你不能真正防範任何事情(比如你對問題的評論中提到的silkfire)。

<?php 
$string = "A great way to start with your theme is to use the XML export file from the demo. Having this starting content makes it easier to see how some features are created. Download the demo content below and import it into your WordPress site to get a replica of the theme demo site.The second step is to navigate to the Tools options and click Import. Then on the bottom of the list."; 

$result = preg_match_all("/(?:\w+\s)+(?:\w+\.\s|\w+\.[a-z](?:\w+\s)+(?:\w+\.)+)/", $string, $matches); 

echo '<pre>'; 
print_r($matches); 
echo '</pre>'; 
?> 

問題是你的字符串,你永遠不知道什麼是「lastword.Wordofnewline」是在代碼邏輯。上面的正則表達式其實內部是一樣的。這將只匹配的情形之一的壽,像這樣的字符串,其中replica變成repli.ca

一個偉大的方式開始與你的主題是從演示使用XML導出文件 。擁有這個起始內容可以更容易地看到如何創建一些功能 。下載下面的演示內容並導入 它變成你的WordPress網站獲得主題演示 site.The第二步的repli.ca是導航到工具選項,然後單擊 導入。然後在列表的底部。

你真的必須基於大量的測試自定義正則表達式,然後才能使其正常工作。如果可能的話(如果是這種情況),你應該讓你的用戶(編寫者)驗證你的代碼的自動編輯輸出:)。

+0

它不工作,因爲每當'.'出現在字符串中時,它就會中斷。 – Toretto 2013-02-20 10:50:21

+0

我已經更新了這個問題。這並不容易,因爲在語言結構中沒有「邏輯」時,人們並不總是把它寫在一致:)。如果新線開始以較低的情況下,並寫入對點你可能更符合這些正則表達式檢查綁定:P – 2013-02-20 12:10:37

0

你需要你的字符串格式化,以便它或者/ n或。在每個句子之後,你可以分割字符串(php explode函數)。任何奇怪的東西像49.99英鎊或'博士約翰史密斯'將打破算法。

+0

拿你來自哪裏,網站(跨度塊的innerHTML取得與捲曲的文本塊一開戰例如)。文本沒有換行符,但您仍然想要找出各個行。你的答案着重於調整源,但我們不知道他是否能控制源字符串的格式。如果那是'是'的話;我想然後他可以去調整他的預代碼,以便取回線後更容易:) – 2013-02-20 12:18:49

相關問題