在多行php中打破长字符串变量

时间:2015-07-24 07:20:08

标签: php html

我创建了一个存储一个非常长的字符串的变量,我想在多行中打印这个变量。

MyShop

上述$ test的输出将是

$test ="This is some example text , I want to print this variable in 3 lines";

注意:我想在每15个字符或每行具有相同字符后添加一个新行。我不想在变量赋值期间添加break标记或任何其他标记。

5 个答案:

答案 0 :(得分:3)

<强> CODEPAD

<?php
$str=  'This is some example text , I want to print this variable in 3 lines, also.';
$x = '12';
$array = explode( "\n", wordwrap( $str, $x));
var_dump($array);
?>

或使用

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

答案 1 :(得分:2)

试试这个

 <?php 
    $test ="This is some example text , I want to print this variable in 3 lines";
    $array = str_split($test, 10); 

    echo implode("<br>",$array);

基于此链接 PHP: split a long string without breaking words

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

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

// Max size of each line
$maxLineLength = 18;

// Auxiliar counters, foreach will use them
$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 :(得分:1)

试试这个

<?php
$test ="This is some example text , I want to print this variable in 3 lines";
$explode=explode(" ",$test);
$String='';
$newString='';
$maxCharacterCount=23;
foreach ($explode as $key => $value) {
    $strlen=strlen($String);
    if($strlen<=$maxCharacterCount){
            $String.=' '.$value;

        }else{
            $newString.=$String.' '.$value.'<br>';
            $String='';

        }
     }
     $finalString= $newString.$String;
     echo $finalString;
?>

答案 3 :(得分:0)

好的,现在一切正常。你只需要在字符数和函数返回表中传递$ string和@maxlenght,并使用cutted string。

  • 不要削减文字,
  • 没有发过maxLenght,

只有一件事就是你需要关心逗号的使用,例如:&#34; word,word&#34;。

$test ="This is some example text, I want to print this variable in 3 lines dasdas das asd asd asd asd asd ad ad";

function breakString($string, $maxLenght) {

    //preparing string, getting lenght, max parts number and so on
$string = trim($string, ' ');
$stringLenght = strlen($string);
$parts = ($stringLenght / $maxLenght );
$finalPatrsNumber = ceil($parts);   
$arrayString = explode(' ', $string);
    //defining variables used to store data into a foreach loop
$partString ='';
$new = array();
$arrayNew = array();

   /**
    * go througt every word and glue it to a $partstring
    * 
    * check $partstring lenght if it exceded $maxLenght 
    * then delete last word and pass it again to $partstring
    * and create now array value
    */

foreach($arrayString as $word){
    $partString.=$word.' ';

    while(  strlen( $partString ) > $maxLenght) {
        $partString = trim($partString, ' ');
        $new = explode(' ', $partString);
        $partString = '';
        $partString.= end($new).' ';  
        array_pop($new);
        //var_dump($new);
        if( strlen(implode( $new, ' ' )) < $maxLenght){
            $value = implode( $new, ' ' );
            $arrayNew[] = $value;
        }        
    }    
}

//    /**
//    * psuh last part of the string into array
//    */
$string2 = implode(' ', $arrayNew);


$string2 = trim($string2, ' ');
$string2lenght = strlen($string2);
$newPart = substr($string, $string2lenght);
$arrayNew[] = $newPart;

  /**
   * return array with broken $parts of $string 
    * $party max lenght is < $maxlenght 
    * and breaks are only after words
   */
return $arrayNew;

}

   /**
    * sample usage
    * @param string $string
    * @param int $maxLenght Description
    */

foreach( breakString($test, 30) as $line){
    echo $line."<br />";
}

显示:

This is some example text, I
want to print this variable
in 3 lines dasdas das asd asd
asd asd asd ad ad

顺便说一句。你可以在这里轻松添加一些规则,如:

  • 最后一行没有单个字符,或者某些单词没有中断等等;

答案 4 :(得分:0)

最简单的解决方案是在新行上使用wordwrap()和explode(),如下所示:

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

其中$ x是要包装字符串的字符数。 复制自here last comment