PHP-如何打印段落中的几个单词

发布于 2024-12-13 04:48:56 字数 187 浏览 5 评论 0原文

在我的代码中,我有:

$row = "Some text in my string";

现在我正在使用 php 并在该变量中打印一些单词。

例如:我想要 2 个单词:输出将是:“Some text”; (等3个词,4个词)

但我不知道如何在php中做到这一点!

In my code, i have:

$row = "Some text in my string";

now i'm using php and print some word in that variable.

for example: i want 2 words: output will be: "Some text"; (etc with 3 words, 4 words)

but i don't know how to do that in php !

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

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

发布评论

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

评论(3

浅浅 2024-12-20 04:48:56

试试这个

function limit_words($string, $word_limit)
    {
        $words = str_word_count($string, 1);
        return implode(" ",array_splice($words,0,$word_limit));
    }


    $content = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

    echo limit_words($content,20);

Try this

function limit_words($string, $word_limit)
    {
        $words = str_word_count($string, 1);
        return implode(" ",array_splice($words,0,$word_limit));
    }


    $content = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

    echo limit_words($content,20);
浊酒尽余欢 2024-12-20 04:48:56
<?php

function firstNWords($inputText, $number)
{
    // using a regular expression to split the inputText by anything that is considered whitespace
    $words = preg_split('~\s~', $inputText, -1, PREG_SPLIT_NO_EMPTY);
    // make sure the number of words we want will not be out of range
    $number = min(count($words), $number);  
    // slice the number of words we want from the array and glue them together with spaces
    return implode(' ', array_slice($words, 0, $number));
}

// loop over the numbers 1..10 and print print some output for test purposes
for ($i = 1; $i < 10; $i ++)
{
    printf("%d: '%s'\n", $i, firstNWords('The Quick brown fox jumps over the lazy dog', $i));
}

输出:

1: 'The'
2: 'The Quick'
3: 'The Quick brown'
4: 'The Quick brown fox'
5: 'The Quick brown fox jumps'
6: 'The Quick brown fox jumps over'
7: 'The Quick brown fox jumps over the'
8: 'The Quick brown fox jumps over the lazy'
9: 'The Quick brown fox jumps over the lazy dog'
<?php

function firstNWords($inputText, $number)
{
    // using a regular expression to split the inputText by anything that is considered whitespace
    $words = preg_split('~\s~', $inputText, -1, PREG_SPLIT_NO_EMPTY);
    // make sure the number of words we want will not be out of range
    $number = min(count($words), $number);  
    // slice the number of words we want from the array and glue them together with spaces
    return implode(' ', array_slice($words, 0, $number));
}

// loop over the numbers 1..10 and print print some output for test purposes
for ($i = 1; $i < 10; $i ++)
{
    printf("%d: '%s'\n", $i, firstNWords('The Quick brown fox jumps over the lazy dog', $i));
}

Outputs:

1: 'The'
2: 'The Quick'
3: 'The Quick brown'
4: 'The Quick brown fox'
5: 'The Quick brown fox jumps'
6: 'The Quick brown fox jumps over'
7: 'The Quick brown fox jumps over the'
8: 'The Quick brown fox jumps over the lazy'
9: 'The Quick brown fox jumps over the lazy dog'
鹿港小镇 2024-12-20 04:48:56

自己动手吧,你将学到 PHP 的一些

技巧:

  1. 分解字符串以获得单词数组。功能-> explode
  2. 获取具有所需字数的数组切片。功能-> array_slice
  3. 连接此数组切片以获取具有所需单词数的字符串。功能-> 内爆

Do it yourself, you will learn some part of PHP

Tips:

  1. Explode the string to get an array of words. function -> explode
  2. take a slice of the array with required number of words. function -> array_slice
  3. join this array slice to get string with required number of words. function -> implode
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文