PHP> echo 字符串由每行 3 个单词分隔?

发布于 2024-10-28 23:05:33 字数 268 浏览 6 评论 0原文

我需要制作一些包含使用爆炸来创建数组的函数的东西。我看过几个例子,但临近结束时我真的很困惑!有没有一种简单易读的方法? (//评论?)

以一段文本为例:

"This is a simple text I just created".

输出应如下所示:

This is a
simple text I
just created 

因此爆炸应将文本分成每行 3 个单词的行。

I need make something that includes a function that uses explode to create an array. I have seen several examples, but near the end I really get confused! Is there a simple readable way for this? (//comments?)

Take for instance a piece of text:

"This is a simple text I just created".

The output should look like this:

This is a
simple text I
just created 

So the explode should split the text into lines of 3 words each.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜血缘 2024-11-04 23:05:33
$text = "This is a simple text I just created";
$text_array = explode(" ", $text);
$chunks = array_chunk($text_array, 3);
foreach ($chunks as $chunk) {
    $line = $impode(" ", $chunk);
    echo $line;
    echo "<br>";
}
$text = "This is a simple text I just created";
$text_array = explode(" ", $text);
$chunks = array_chunk($text_array, 3);
foreach ($chunks as $chunk) {
    $line = $impode(" ", $chunk);
    echo $line;
    echo "<br>";
}
风启觞 2024-11-04 23:05:33

试试这就是你需要的:

<?php

$text = "This is a simple text I just created";

$text_array = explode(' ', $text);

$i = 1; // I made change here :)
foreach($text_array as $key => $text){

 if(ceil(($key + 1) / 3) != $i) { echo "<br/>"; $i = ceil(($key + 1) / 3);  }
 echo $text.' ';
}
?>

结果:

This is a
simple text I
just created 

Try this is what you need:

<?php

$text = "This is a simple text I just created";

$text_array = explode(' ', $text);

$i = 1; // I made change here :)
foreach($text_array as $key => $text){

 if(ceil(($key + 1) / 3) != $i) { echo "<br/>"; $i = ceil(($key + 1) / 3);  }
 echo $text.' ';
}
?>

Result:

This is a
simple text I
just created 
筱武穆 2024-11-04 23:05:33

使用 substr() 函数 link

示例:

<?php
$rest = substr("abcdef", -1);    // returns "f"
$rest = substr("abcdef", -2);    // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
?>

在您的案件:

<?php 
$rest = substr("This is a simple text I just created", 0, 15); //This will return first 15 characters from your string/text

echo $rest; // This is a simpl
?>

Use substr() function link

Example:

<?php
$rest = substr("abcdef", -1);    // returns "f"
$rest = substr("abcdef", -2);    // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
?>

In your case:

<?php 
$rest = substr("This is a simple text I just created", 0, 15); //This will return first 15 characters from your string/text

echo $rest; // This is a simpl
?>
归属感 2024-11-04 23:05:33

爆炸只是在指定字符处分割字符串。没有更多的事情了。

爆炸(',','文本,去这里');

每当遇到 a 时,就会分割字符串,并返回一个数组。

用空格字符分隔

explode(' ', '文本在此处');

这只由空格字符分割,而不是全部空格。 Preg_split 更容易被任何空格分割

explode just splits the string at a specified character. There's nothing more to it.

explode(',', 'Text,goes,here');

This splits the string whenever it meets a , and returns an array.

to split by a space character

explode(' ', 'Text goes here');

This splits only by a space character, not all whitespace. Preg_split would be easier to split by any whitespace

俯瞰星空 2024-11-04 23:05:33

所以像...

function doLines($string, $nl){
  // Break into 'words' 
  $bits = explode(' ', $string);
  $output = '';
  $counter=0;
  // Go word by word...
  foreach($bits as $bit){
     //Add the word to the output...
     $output .= $bit.' ';
     //If it's 3 words...
     if($counter==2){
       // Remove the trailing space
       $output = substr($output, 0, strlen($output)-1);
       //Add the separator character...
       $output .=$nl;
       //Reset Counter
       $counter=0;
     }
  }
  //Remove final trailing space
  $output = substr($output, 0, strlen($output)-1);

  return $output;
}

那么您所要做的就是:

echo doLines("This is a simple text I just created", "\n");

echo doLines("This is a simple text I just created", "<br />");

..取决于您是否只想要新行或者是否想要 HTML 输出。

So something like...

function doLines($string, $nl){
  // Break into 'words' 
  $bits = explode(' ', $string);
  $output = '';
  $counter=0;
  // Go word by word...
  foreach($bits as $bit){
     //Add the word to the output...
     $output .= $bit.' ';
     //If it's 3 words...
     if($counter==2){
       // Remove the trailing space
       $output = substr($output, 0, strlen($output)-1);
       //Add the separator character...
       $output .=$nl;
       //Reset Counter
       $counter=0;
     }
  }
  //Remove final trailing space
  $output = substr($output, 0, strlen($output)-1);

  return $output;
}

Then all you have to is:

echo doLines("This is a simple text I just created", "\n");

or

echo doLines("This is a simple text I just created", "<br />");

..depending if you just want new lines or if you want HTML output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文