Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-如何在段落中打印几个单词_Php_String_Echo_Words - Fatal编程技术网

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

PHP-如何在段落中打印几个单词,php,string,echo,words,Php,String,Echo,Words,在我的代码中,我有: $row = "Some text in my string"; 现在我使用php并在该变量中打印一些单词 例如:我想要2个字:输出将是:“一些文字”;(等3个字,4个字) 但我不知道如何在php中做到这一点 自己动手,你会学到PHP的一些内容 小贴士: 分解字符串以获得单词数组。函数->分解 从数组中选取一个具有所需字数的片段。函数->array\u slice 加入此数组片段以获取所需字数的字符串。函数->内爆 试试这个 function limit_words($s

在我的代码中,我有:

$row = "Some text in my string";
现在我使用php并在该变量中打印一些单词

例如:我想要2个字:输出将是:“一些文字”;(等3个字,4个字)


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

自己动手,你会学到PHP的一些内容

小贴士:

  • 分解字符串以获得单词数组。函数->
    分解
  • 从数组中选取一个具有所需字数的片段。函数->
    array\u slice
  • 加入此数组片段以获取所需字数的字符串。函数->
    内爆
  • 试试这个

    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);
    

    您的字符串是否也可以包含字符,如“.”、“.”、“?”等等?您提出的问题,正如目前编写的,有许多可能的解决方案。也许如果你能更准确地说出你想要的,我们可以发布更精确的答案。我只想在一段中打印固定的单词。好的,非常感谢!但是我的输出语言有点问题。我现在的输出:“布鲁克豪斯学院l� tr� � ng n� 我Tru Chuiên gi� ng d� y c� c ch� � ng tr� nh trung h� c ph� th� ng GCSE d� "; 这是为什么?您的新问题是因为您的浏览器不知道脚本输出的正确字符编码。这是另一个问题。感谢您的回答,我使用的是firefox 7.0.1和字符集Unicode UTF-8。我的越南语输出(我是越南语)…我如何解决此问题?
    <?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'