2012-04-13 106 views
3

我发现这个PHP脚本:分割字符串PHP

//带标签,以避免破坏任何HTML $字符串= 用strip_tags($字符串);

如果(strlen的($字符串)> 500){

// truncate string 
$stringCut = substr($string, 0, 500); 

// make sure it ends in a word so assassinate doesn't become ass... 
$string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="/this/story">Read More</a>'; } echo $string; 

通过webbiedave创建。现在我的问题是我如何在strrpos中指定空间或点?

所以,如果我限制串22,如果我有这样的事情计算器是最好的网站不断 - >将输出的StackOverflow是......如果我有这样的事情http://stackoverflow.com是最好的网站不断 - >将输出只...因为他没有找到空间和字符串长度> 22。如果在字符串中找到一个点,如何修改此脚本以剪切文本,因此http://stackoverflow.com是有史以来最好的网站 - >成为http://stackoverflow ...?

回答

1

竟被我d从结尾检查字符串,原因您需要删除不完整的单词。该不完整单词的条件是当短字符串不以空格结尾或结尾字符如'!'时或者只是点。接下来的cond就是看字符串结束后是否+1符号也是这样的字符。如果是这样的话,你只需要删除从最后到下一个空格的任何字符。这可以通过正则表达式来完成(类似于/ [:alfa] + $ /的东西,似乎要好得多)。

这是一个简单的方法来做基本的事情,但我认为一个好的开始。

实例的可能是什么?

function word_wrap_custom($string, $limit){ 
$oneWord = explode(' ', $string); 
if(count($oneWord) < 2) 
    return $oneWord[0]; 
$string = substr($string, 0, $limit + 2); 

$endchar = substr($string, $limit, $limit + 1); 

$postendchar = substr($string, $limit + 1, $limit + 2); 

$arrAccetpEndChar = array(' ', '!', '?', ',', '.', ';', ':'); 

if(in_array($postendchar, $arrAccetpEndChar) || in_array($endchar, $arrAccetpEndChar)) 
{ 
    return $string; 
} 
else 
{ 
    return preg_replace('/[A-Za-z0-9]+$/', '', $string); 
} 
} 
+0

你的答案似乎是最好的:)但我是一个在PHP的begginer ...你能翻译你说的在PHP代码中的? – TGeorge 2012-04-13 09:10:50

+0

上面贴的代码。你将不得不对它进行编辑,以便完成你想要的功能。如果您有任何问题,请询问。 – 2012-04-13 09:59:55

+0

这是工作,但你的代码有一个bug。如果我没有空间它只会输出...阅读更多 如果我有像 echo word_wrap_custom(“Stackoverflow”,“22”);?>将只输出...阅读更多,如果我把preg_replace('/ [A-Za-z0-9] + $ /','... read more',$ string);我们该如何解决这个问题? – TGeorge 2012-04-13 10:15:07

2

如果您想在不破坏单词的情况下进行拆分,但在尊重线条限制的情况下,请使用wordwrap而不是其他任何拆分方法。

$longText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ut purus a tellus ultrices vulputate. Aliquam posuere facilisis elit ut adipiscing. Nunc auctor dignissim porta. Vestibulum vitae tempor augue. Nam vel odio quis quam gravida ultrices sed a arcu. Phasellus nec odio massa. Duis imperdiet rutrum mi, vitae volutpat nulla convallis quis. Donec dignissim pulvinar mauris id molestie. Duis id mauris augue, id sagittis velit. Ut justo lectus, scelerisque egestas tempor et, facilisis vitae erat. Quisque ut mattis nulla. Donec a justo quis nisi tempus ultrices. Phasellus non dui non dolor tristique tincidunt vitae imperdiet libero. Pellentesque pretium luctus sem."; 

$makeLine = wordwrap($longText, 50, PHP_EOL); 

echo $makeLine; 

前后:http://codepad.org/Dqz8qzAy

如果你只是想在第一行,也许作为一个摘要文本,你可能会爆炸所产生的字符串,第一个结果出所得阵列的转移:

$longText = "..."; 

$makeLine = wordwrap($longText, 50, '\r\n'); 
$firstSen = array_shift(explode('\r\n', $makeLine)); 

echo $firstSen; // Lorem ipsum dolor sit amet, consectetur adipiscing... 
0

这对不会打破的话:

function ShortenText($text, $chars) 
{ 
    $chars = $chars;$text = $text." "; 
    $countchars = strlen($text); 
    if($countchars > $chars) 
    { 
     $text = substr($text,0,$chars); 
     $text = substr($text,0,strrpos($text,' ')); 
     $text = $text."..."; 
    } 
    return $text; 
} 
+0

你的代码不相同,一个由me.So发布,如果我有 将输出StackOverFlow是...如果我有 <?php回声ShortenText(”http://stackoverflow.com是有史以来最好的网站“,”22“);?>将只输出...因为他在前22个字符中找不到空格。所以我想要搜索点或空间。 – TGeorge 2012-04-13 09:46:24