2012-06-29 24 views
14

Tôi đang tìm một cái gì đó dọc theo dòngPHP: chia một chuỗi dài mà không có lời phá vỡ

str_split_whole_word($longString, x) 

nơi $longString là một tập hợp các câu, và x là độ dài ký tự cho mỗi dòng. Nó có thể được khá dài, và tôi muốn về cơ bản chia nó thành nhiều dòng trong các hình thức của một mảng.

Vì vậy, ví dụ,

$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... 
) 

Trả lời

12

giải pháp này đảm bảo rằng các dòng sẽ được tạo ra mà không cần lời nói phá vỡ, những gì bạn sẽ không nhận được sử dụng WordWrap(). Nó sẽ sử dụng không gian để phát nổ chuỗi và sau đó sử dụng một foreach để lặp mảng và tạo ra các dòng mà không vi phạm công trình và với độ dài tối đa được xác định bằng cách sử dụng $maxLineLength. Dưới đây là mã, tôi đã thực hiện một số kiểm tra và nó hoạt động tốt.

$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, cảm ơn sự giúp đỡ của bạn. Điều này cũng giống như bạn đã mô tả! – musicliftsme

+0

@ user796837, Nevermind, tôi rất vui được giúp bạn! –

38

Giải pháp đơn giản nhất là sử dụng wordwrap()explode() trên dòng mới, như vậy:

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

đâu $x là một số ký tự để quấn chuỗi trên.

+10

Đây là đơn giản như vậy và đáng nhớ; nên là câu trả lời được chấp nhận! –

+2

Điều này sẽ làm việc tuyệt vời nếu bạn không có ngắt dòng trong chuỗi. – BakerStreetSystems

7

Sử dụng wordwrap() để chèn xuống hàng, sau đó explode() trên những linebreaks:

// 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

Chính xác những gì tôi muốn :) Cảm ơn! – Harsha

1

Made chức năng từ Marcio Simao bình luận

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

Hãy thử chức năng này .......

<?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; 
} 
?> 

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

+1

Ít nhất hãy thêm tín dụng cho điều đó: http://www.ebrueggeman.com/blog/abbreviate-text-without-cutting-words-in-half –

Các vấn đề liên quan