2013-04-29 134 views
0

有没有办法在PHP中修剪文本字符串,使其具有一定数量的字符?举例来说,如果我有字符串:缩短PHP中的文本字符串

$string = "this is a string";

我怎么能修剪说:

$newstring = "this is";

这是我到目前为止,使用chunk_split(),但它不是”不工作。任何人都可以改进我的方法吗?

function trimtext($text) 
{ 
$newtext = chunk_split($text,15); 
return $newtext; 
} 

我也看了this的问题,但我真的不明白。

+0

可能的重复http://stackoverflow.com/q/15705059/ – HamZa 2013-04-29 12:37:23

+0

不,我没有使用正则表达式。 – imulsion 2013-04-29 12:38:28

+0

接受的答案不是正则表达式的解决方案:) – HamZa 2013-04-29 12:38:53

回答

4
if (strlen($yourString) > 15) // if you want... 
{ 
    $maxLength = 14; 
    $yourString = substr($yourString, 0, $maxLength); 
} 

将完成这项工作。

看一看here

+0

你能解释第二个参数吗? – imulsion 2013-04-29 12:37:01

+0

@imulsion:第二个参数是“切”字符串的长度) – DonCallisto 2013-04-29 12:38:01

+0

谢谢!接受,当它让我。愚蠢的十分钟等待期 – imulsion 2013-04-29 12:39:45

3

您可以使用此

substr() 

函数获取串

4
function trimtext($text, $start, $len) 
{ 
    return substr($text, $start, $len); 
} 

你可以这样调用该函数:

$string = trimtext("this is a string", 0, 10); 

将返回:

This is a

2

如果你想获得一个字符串有一定数量就可以使用SUBSTR字符,即

$newtext = substr($string,0,$length); 

其中$长度为新的字符串的给定长度。

0

如果你想第一个10个字的摘要(您可以在$文本中使用HTML,脚本之前还有用strip_tags) 使用此代码:

preg_match('/^([^.!?\s]*[\.!?\s]+){0,10}/', strip_tags($text), $abstract); 
echo $abstract[0]; 
3

你没有说这样做的原因但想想你想达到什么。这里有一个功能可以缩短一个单词的字符串,最后可以加或不加椭圆:

function limitStrlen($input, $length, $ellipses = true, $strip_html = true) { 
    //strip tags, if desired 
    if ($strip_html) { 
     $input = strip_tags($input); 
    } 

    //no need to trim, already shorter than trim length 
    if (strlen($input) <= $length) { 
     return $input; 
    } 

    //find last space within length 
    $last_space = strrpos(substr($input, 0, $length), ' '); 
    if($last_space !== false) { 
     $trimmed_text = substr($input, 0, $last_space); 
    } else { 
     $trimmed_text = substr($input, 0, $length); 
    } 
    //add ellipses (...) 
    if ($ellipses) { 
     $trimmed_text .= '...'; 
    } 

    return $trimmed_text; 
} 
+1

最后正确答案** + 1 ** – HamZa 2013-04-29 13:04:48

2

substr减半字。如果单词包含UTF8字符,它会变得非常糟糕。因此,这将更好地使用mb_substr:

$string = mb_substr('word word word word', 0, 10, 'utf8').'...';

0

我的功能有一定的长度,但我喜欢用它。我将字符串int转换为数组。

function truncate($text, $limit){ 
    //Set Up 
    $array = []; 
    $count = -1; 
    //Turning String into an Array 
    $split_text = explode(" ", $text); 
    //Loop for the length of words you want 
    while($count < $limit - 1){ 
     $count++; 
     $array[] = $split_text[$count]; 
    } 
    //Converting Array back into a String 
    $text = implode(" ", $array); 

    return $text." ..."; 

    } 

或者如果文本来自编辑器并且您想要去掉HTML标记。

function truncate($text, $limit){ 
    //Set Up 
    $array = []; 
    $count = -1; 
    $text = filter_var($text, FILTER_SANITIZE_STRING); 

    //Turning String into an Array 
    $split_text = preg_split('/\s+/', $text); 
    //Loop for the length of words you want 
    while($count < $limit){ 
     $count++; 
     $array[] = $split_text[$count]; 
    } 

    //Converting Array back into a String 
    $text = implode(" ", $array); 

    return $text." ..."; 

    }