php - 如何使用爆炸在每三个空间之后拆分?

标签 php

我有一个如下所示的字符串:

$str = "In order to successfully build your backlinks in a highly competitive industry (or if you're targeting highly competitive keywords), you have to get smart about your strategy. That means using the best back-link building tools available";

现在我想在每三个单词之后拆分字符串。这就是我想要的..

split1 = in order to
split2 = successfully build your
split4 = backlinks in a

依此类推,直到字符串结束。

我已经使用 preg_match_all 完成了它,但它没有给我想要的东西。那么有人可以帮助我使用 split() 或 preg_split 或 explode() 函数拆分字符串。

谢谢

最佳答案

$split = explode(' ', $str); // Split up the whole string
$chunks = array_chunk($split, 3); // Make groups of 3 words
$result = array_map(function($chunk) { return implode(' ', $chunk); }, $chunks); // Put each group back together

DEMO

$result 是:

Array
(
    [0] => In order to
    [1] => successfully build your
    [2] => backlinks in a
    [3] => highly competitive industry
    [4] => (or if you're
    [5] => targeting highly competitive
    [6] => keywords), you have
    [7] => to get smart
    [8] => about your strategy.
    [9] => That means using
    [10] => the best back-link
    [11] => building tools available
)

关于php - 如何使用爆炸在每三个空间之后拆分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666117/

相关文章:

php和mysql搜索用户

php - 获取sql中的中文字母以在php中显示

php - 如果表 1 和表 2 或表 3 具有相同的 ID,则显示名称

php - 调用 mysqli_error() 时出现警告

php - 如何调用 protected 函数?

php - 如何解决 Symfony2 部署问题?

php - 将动态表单数据插入数据库。 PHP

php - 带有空格的自定义列名称 Laravel 4

PHP如何将eval(0分配给变量

php - SQL - 条件 WHERE