2012-11-28 83 views
2

下面的文本變量包含10個句子。如何從php中的變量中提取句子或字數?

$txt = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.'; 

如果變量包含值從誰進入在例如一個textarea一堆文本的用戶來了,我怎麼知道該變量多少段落包含?

另外,我如何提取一定數量的單詞的變量,比如說最多爲100個單詞,但如果這100個單詞的計數沒有以完整的句子結束,那麼腳本會給出包含100個單詞的完整句子或在完整的句子中,有多少單詞接近我設置的100個字數的限制。

這是可能的在PHP?

+1

試試這個http://stackoverflow.com/questions/79960/how-to-truncate-a-string-in-php-to-the-word-closest-to-a-certain-number-的,恰拉 – Sibu

回答

0
  1. 查找總長度,並通過預定義的值的段落
  2. 使用substr (string $string , int $start [, int $length ]

也見將其劃分:substr function

0

你可以做這樣的事情。

$input = $_POST['textarea']; 
$output = array(); 
preg_match_all("/<p>(.*)<\/p>/",$input,$output); 
$paragraphs = count($output); 
相關問題