2013-12-17 57 views
0

試圖創建一個省略號只顯示部分字符串首先和字符串的其餘部分後點擊功能。php省略號,整個單詞的開始和結束與preg_replace

發現許多教程來創建ellispis函數,但試圖很長時間如何從結束部分獲取整個單詞。

我一直試圖做這樣的

<?php 

    $text="Lorem ipsum dolor sit amet."; 
    //  123456789 
    echo substr($text,0,9); // result: "Lorem ips" 
    echo '<hr>'; 

    $start = substr($text,0,9); 
    // now this preg_replace() is awesome cause its only returning the entire word 
    echo preg_replace('/\w+$/','',$start); //result: "Lorem" 

    echo '<hr>'; 
    echo substr($text,9,strlen($text)); //result: "um dolor sit amet." 

    // now how should this preg_replace be to get result "ipsum dolor sit amet." 

?> 

所以,問題是:如何把這個preg_replace()被用來獲取引起"ipsum dolor sit amet."

我試圖改變像preg_replace('/\$+w/','',$start);這樣的東西,但我不知道如何編寫該正則表達式。

回答

2
preg_replace('/^\w+\s/','',$text) 
+0

真棒謝謝你!你有任何鏈接或資源來學習如何編寫這些正則表達式? – caramba

+2

PHP Live Regex - http://www.phpliveregex.com/ – Sorbo

+0

@caramba:要學習正則表達式,這是最好的教程之一:http://www.regular-expressions.info/tutorial.html – Enissay

0

Sorbos的答案在我的問題上完全正確。由於我的問題不是很清楚,我必須改變答案才能得到我需要的結果。問題在於字符串可能從任何地方開始(給定位置)。所以,我仍然不知道如果這是可能的了preg_replace()

這給了我同樣的結果來解決:

$count=9; 
$rest = substr($text,$count,strlen($text)); 
while(substr($rest, 0,1)!=' ') { 
    $rest = substr($text,$count,strlen($text)); 
    $count--; 
} 
echo $rest; 

如果任何人有一個更好的解決方案隨意張貼。 謝謝!

相關問題