简体   繁体   English

“自动换行”功能

[英]"word-wrap" functionality

I need to develop a PHP functionality where I have an array and a maximum character count.我需要开发一个 PHP 功能,其中我有一个数组和一个最大字符数。

Return a collection of strings where each string element represents a line that contains as many words as possible, with the words in each line being concatenated with a single '-'.返回一个字符串集合,其中每个字符串元素代表包含尽可能多的单词的一行,每行中的单词用单个“-”连接。 The length of each string must not exceed the maximum character length per line.每个字符串的长度不得超过每行的最大字符长度。

Your function should take in the maximum characters per line and return a data structure representing all lines in the indicated max length.您的 function 应该接受每行的最大字符数,并返回一个数据结构,表示指定最大长度的所有行。

Examples:例子:

words1 = [ "The", "day", "began", "as", "still", "as", "the",
          "night", "abruptly", "lighted", "with", "brilliant",
          "flame" ]

wrapLines(words1, 13) "wrap words1 to line length 13" => wrapLines(words1, 13) "将 words1 换行到行长 13" =>

[ "The-day-began",
    "as-still-as",
    "the-night",
    "abruptly",
    "lighted-with",
    "brilliant",
    "flame" ]

What I tried so far is:到目前为止我尝试的是:

foreach ($words1 as $i => $word) {
            $a = '';
            if($i!= 0 && strlen($a) <= $lineLength1_1){
                 $a = $a.'-'.$word;
            } else {
                 $a = $word;
            }
            echo $a;
        }

The result I get is.我得到的结果是。

The-day-began-as-still-as-the-night-abruptly-lighted-with-brilliant-flame

Can someone please help me how to check for the count based on condition and then break into next array key as result?有人可以帮我如何根据条件检查计数然后进入下一个数组键吗? Thanks.谢谢。

You can use PHP's wordwrap() function to help with this:您可以使用 PHP 的wordwrap() function 来帮助解决这个问题:

// $a is an array of strings, $line_length is the max line length
function wrap( $a, $line_length ) {          
    $r = array_map( 
        function($s) {
            return str_replace( ' ', '-', $s ); // Replace spaces with dashes
        },
        // Rewrap $a by first joining it into a single string, then explode on newline chars into a new array
        explode( "\n", wordwrap(join(' ', $a), $line_length ) )
    );
    
    return $r;
}

$words1 = [ "The", "day", "began", "as", "still", "as", "the",
          "night", "abruptly", "lighted", "with", "brilliant",
          "flame" ];

print_r( wrap( $words1, 13 ) );

Results:结果:

Array
(
    [0] => The-day-began
    [1] => as-still-as
    [2] => the-night
    [3] => abruptly
    [4] => lighted-with
    [5] => brilliant
    [6] => flame
)

If your teacher disallows you to use built-in PHP function such as wordwrap(), then you may use the following:如果你的老师不允许你使用内置的 PHP function 比如 wordwrap(),那么你可以使用下面的:

  1. loop over the array遍历数组
  2. concat each element with previous one if the string length is less than the maximum allowed value (eg 13)如果字符串长度小于最大允许值(例如 13),则将每个元素与前一个元素连接
  3. push the concatenated string into another array (eg $words2[])将连接后的字符串推入另一个数组(例如 $words2[])
  4. return the $words2 array as the result返回 $words2 数组作为结果

So you can use the function wrapLines($words1,13) , as follows:所以你可以使用 function wrapLines($words1,13) ,如下:

<?php

function wrapLines($words, $maxLineLength) {
$words2=[];
$words1=$words;

$index=0;
$maxlength=$maxLineLength;
$injectstring="";
$olstring="";

while  ($index < count($words1)){

    if ($injectstring==""){
       $tempstring=$words1[$index];
    }else{
       $tempstring="-".$words1[$index];
    }


   $oldstring=$injectstring;

  if (strlen($injectstring) < $maxlength) {
      $injectstring=$injectstring . $tempstring;
  }

  if (strlen($injectstring) == $maxlength) {
       $words2[]= $injectstring ; 
       $injectstring=""; 
   }

  if (strlen($injectstring) > $maxlength) {     
       $words2[]= $oldstring ; 
       $injectstring=$words1[$index]; 
   }
$index++;
}
   $words2[]= $injectstring ."<br>"; 
return($words2); 
}



$words1 = [ "The", "day", "began", "as", "still", "as", "the",
          "night", "abruptly", "lighted", "with", "brilliant",
          "flame" ];

print_r(wrapLines($words1,13));
          ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM