2012-06-29 46 views
14

我在尋找沿PHP:沒有打破的話分割的長字符串

str_split_whole_word($longString, x) 

所在行$longString是句子的集合東西,x是每行的字符長度。它可能相當長,我想基本上將它分成多行,形式爲數組。

因此,例如,

$longString = 'I like apple. You like oranges. We like fruit. I like meat, also.'; 
$lines = str_split_whole_word($longString, x); 

$lines = Array(
    [0] = 'I like apple. You' 
    [1] = 'like oranges. We' 
    [2] = and so on... 
) 

回答

12

該解決方案可確保行會不會破壞詞來創建的,你不會得到使用換行()。 它將使用空格來爆炸字符串,然後使用foreach來循環數組並創建線條而不破壞作品,並使用$maxLineLength定義最大長度。下面是代碼,我做了一些測試,它工作正常。

$longString = 'I like apple. You like oranges. We like fruit. I like meat, also.'; 

$arrayWords = explode(' ', $longString); 

$maxLineLength = 18; 

$currentLength = 0; 
$index = 0; 

foreach ($arrayWords as $word) { 
    // +1 because the word will receive back the space in the end that it loses in explode() 
    $wordLength = strlen($word) + 1; 

    if (($currentLength + $wordLength) <= $maxLineLength) { 
     $arrayOutput[$index] .= $word . ' '; 

     $currentLength += $wordLength; 
    } else { 
     $index += 1; 

     $currentLength = $wordLength; 

     $arrayOutput[$index] = $word; 
    } 
} 
+2

Marcio,謝謝你的幫助。這有助於你的描述! – musicliftsme

+0

@ user796837,Nevermind,很高興爲您效勞! –

38

最簡單的解決辦法是在新的生產線使用wordwrap(),並explode(),像這樣:

$array = explode("\n", wordwrap($str, $x)); 

$x是一個數字的字符來包裝的字符串開啓。

+10

這是如此簡單難忘;應該是被接受的答案! –

+2

如果您在字符串中沒有換行符,這會很好用。 – BakerStreetSystems

7

使用wordwrap()插入換行符,然後explode()這些換行:

// Wrap at 15 characters 
$x = 15; 
$longString = 'I like apple. You like oranges. We like fruit. I like meat, also.'; 
$lines = explode("\n", wordwrap($longString, $x)); 

var_dump($lines); 
array(6) { 
    [0]=> 
    string(13) "I like apple." 
    [1]=> 
    string(8) "You like" 
    [2]=> 
    string(11) "oranges. We" 
    [3]=> 
    string(13) "like fruit. I" 
    [4]=> 
    string(10) "like meat," 
    [5]=> 
    string(5) "also." 
} 
+0

正是我想要的:)謝謝! – Harsha

1

製造功能馬爾西奧思茅評論

function explodeByStringLength($string,$maxLineLength) 
{ 
    if(!empty($string)) 
    { 
     $arrayWords = explode(" ",$string); 

     if(count($arrayWords) > 1) 
     { 
      $maxLineLength; 
      $currentLength = 0; 

      foreach($arrayWords as $word) 
      { 
       $wordLength = strlen($word); 
       if(($currentLength + $wordLength) <= $maxLineLength) 
       { 
        $currentLength += $wordLength; 
        $arrayOutput[] = $word; 
       } 
       else 
       { 
        break; 
       } 
      } 

      return implode(" ",$arrayOutput); 
     } 
     else 
     { 
      return $string; 
     }  
    } 
    else return $string; 
} 
1

嘗試這個功能.......

<?php 
/** 
* trims text to a space then adds ellipses if desired 
* @param string $input text to trim 
* @param int $length in characters to trim to 
* @param bool $ellipses if ellipses (...) are to be added 
* @param bool $strip_html if html tags are to be stripped 
* @param bool $strip_style if css style are to be stripped 
* @return string 
*/ 
function trim_text($input, $length, $ellipses = true, $strip_tag = true,$strip_style = true) { 
    //strip tags, if desired 
    if ($strip_tag) { 
     $input = strip_tags($input); 
    } 

    //strip tags, if desired 
    if ($strip_style) { 
     $input = preg_replace('/(<[^>]+) style=".*?"/i', '$1',$input); 
    } 

    if($length=='full') 
    { 

     $trimmed_text=$input; 

    } 
    else 
    { 
     //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), ' '); 
     $trimmed_text = substr($input, 0, $last_space); 

     //add ellipses (...) 
     if ($ellipses) { 
     $trimmed_text .= '...'; 
     }  
    } 

    return $trimmed_text; 
} 
?> 

信用:http://www.ebrueggeman.com/blog/abbreviate-text-without-cutting-words-in-half

+1

至少爲此添加功勞:http://www.ebrueggeman.com/blog/abbreviate-text-without-cutting-words-in-half –