PHP:爆炸后合并文本

发布于 2024-09-16 16:45:10 字数 272 浏览 8 评论 0原文

我使用爆炸将文本分成几部分,然后使用 foreach 在文本中查找一些内容。

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

foreach ($pieces as $piece) {
    Some Modification of the piece
}

我的问题是如何将这些碎片重新组合在一起?这样我就可以对文本进行自动换行。 有些像这样:

piece 1 + piece 2 + etc

Using explode I broke up the text into pieces, then us the foreach to look for a few thing in the text.

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

foreach ($pieces as $piece) {
    Some Modification of the piece
}

My questions so how can I put those pieces back together? So I can wordwrap the text.
Some like this:

piece 1 + piece 2 + etc

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

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

发布评论

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

评论(6

深海不蓝 2024-09-23 16:45:10

您将使用 implode() 函数。

http://php.net/manual/en/function.implode.php

string implode ( string $glue , array $pieces )
string implode ( array $pieces )

编辑:可能是有史以来最具误导性的问题。

如果您尝试对正在构建的图像进行自动换行,也许您可​​以使用 float:left 样式将它们全部放入单独的 div 中。

You would use the implode() function.

http://php.net/manual/en/function.implode.php

string implode ( string $glue , array $pieces )
string implode ( array $pieces )

EDIT: Possibly the most misleading question ever.

If you're trying to word wrap the images you're constructing, perhaps you could put them all in individual div's in order with the float:left style.

友谊不毕业 2024-09-23 16:45:10

这是我的看法

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ornare tincidunt euismod. Pellentesque sodales elementum tortor posuere mollis. Curabitur in sem eu urna commodo vulputate.\nVivamus libero velit, auctor accumsan commodo vel, blandit nec turpis. Sed nec dui sit amet velit interdum tincidunt.";

// Break apart at new lines.
$pieces = explode("\n", $text);

// Use reference to be able to modify each piece.
foreach ($pieces as &$piece)
{
    $piece = wordwrap($piece, 80);
}

// Join the pieces together back into one line.
$wrapped_lines = join(' ', $pieces);

// Convert new lines \n to <br>.
$wrapped_lines = nl2br($wrapped_lines);
echo $wrapped_lines;

/* Output:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ornare tincidunt<br />
euismod. Pellentesque sodales elementum tortor posuere mollis. Curabitur in sem<br />
eu urna commodo vulputate. Vivamus libero velit, auctor accumsan commodo vel, blandit nec  turpis. Sed nec<br />
*/

Here's my take

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ornare tincidunt euismod. Pellentesque sodales elementum tortor posuere mollis. Curabitur in sem eu urna commodo vulputate.\nVivamus libero velit, auctor accumsan commodo vel, blandit nec turpis. Sed nec dui sit amet velit interdum tincidunt.";

// Break apart at new lines.
$pieces = explode("\n", $text);

// Use reference to be able to modify each piece.
foreach ($pieces as &$piece)
{
    $piece = wordwrap($piece, 80);
}

// Join the pieces together back into one line.
$wrapped_lines = join(' ', $pieces);

// Convert new lines \n to <br>.
$wrapped_lines = nl2br($wrapped_lines);
echo $wrapped_lines;

/* Output:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ornare tincidunt<br />
euismod. Pellentesque sodales elementum tortor posuere mollis. Curabitur in sem<br />
eu urna commodo vulputate. Vivamus libero velit, auctor accumsan commodo vel, blandit nec  turpis. Sed nec<br />
*/
在你怀里撒娇 2024-09-23 16:45:10

首先,如果你想在内联循环中修改每个$piece,你必须将这些项目作为引用进行循环:

foreach ($pieces as &$piece)

当循环完成时,你可以产生使用 join() 再次生成单个字符串:(

$string = join(' ', $pieces);

join() 的第一个参数是将各部分粘合在一起的分隔符。使用最适合您的应用程序的内容。)

First of all, if you want to modify each $piece in the loop inline, you have to loop over the items as references:

foreach ($pieces as &$piece)

When the loop is finished, you can produce a single string again by using join():

$string = join(' ', $pieces);

(The first parameter to join() is the separator that glues the pieces together. Use whatever fits your application best.)

水晶透心 2024-09-23 16:45:10

到目前为止的所有答案都使它变得比实际情况要困难得多。为什么不在修改时将其放回原处呢?我想这就是您正在寻找的。

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

// text has already been passed to pieces so unset it
unset($text);

foreach ($pieces as $piece) {
    Some Modification of the piece
    // rebuild the text here
    $text .= {MODIFIED PIECE};
}

// print the new modified version
echo $text;

All of the answers so far make it much more difficult than it is. Why not put it back together as you modify it? I think this is what you are looking for.

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

// text has already been passed to pieces so unset it
unset($text);

foreach ($pieces as $piece) {
    Some Modification of the piece
    // rebuild the text here
    $text .= {MODIFIED PIECE};
}

// print the new modified version
echo $text;
扛刀软妹 2024-09-23 16:45:10

$pieces = implode(' ', $pieces);

$pieces = implode(' ', $pieces);

花开半夏魅人心 2024-09-23 16:45:10

问题很令人困惑,但是这样的事情会起作用吗:

$new = implode(' ', $pieces);
echo wordwrap($new, 120); // wordwrap 120 chars

Question is quite confusing, but would something like this work:

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