php - 为什么 PHP 忽略任何字符串开头的换行符

标签 php arrays string laravel newline

我期望来自 Laravel 微服务的请求具有用户输入文本。

我遇到的情况是,用户在他们编写的段落开头输入了多个换行符。

代码应根据换行符“拆分”该文本并单独处理每个段落。

例如:我在请求中有这个字符串:

JSON:
{
    "text": "\n\n\n\n\n\nHere you can find activities to practise your reading skills. Reading will help you to improve your understanding of the language and build your vocabulary.\n\nThe self-study lessons in this section are written and organised according to the levels of the Common European Framework of Reference for languages (CEFR).\n\nThere are different types of texts and interactive exercises that practise the reading skills you need to do well in your studies, to get ahead at work and to communicate in English in your free time."
}

我希望得到这个数组:

Array
(
    [0] => Array
        (
            [0] =>  
        )
    [1] => Array
        (
            [0] =>  
        )
    [2] => Array
        (
            [0] =>  
        )
    [3] => Array
        (
            [0] =>  
        )
    [4] => Array
        (
            [0] =>  
        )
    [5] => Array
        (
            [0] =>  
        )
    [6] => Array
        (
            [0] => Here you can find activities to practise your reading skills. Reading will help you to improve your understanding of the language and build your vocabulary.
        )

    [7] => Array
        (
            [0] =>  
        )

    [8] => Array
        (
            [0] => The self-study lessons in this section are written and organised according to the levels of the Common European Framework of Reference for languages (CEFR).
        )

    [9] => Array
        (
            [0] =>  
        )

    [10] => Array
        (
            [0] => There are different types of texts and interactive exercises that practise the reading skills you need to do well in your studies, to get ahead at work and to communicate in English in your free time.
        )

)

但不幸的是,我有这个数组:

Array
(
    [0] => Array
        (
            [0] => Here you can find activities to practise your reading skills. Reading will help you to improve your understanding of the language and build your vocabulary.
        )

    [1] => Array
        (
            [0] =>  
        )

    [2] => Array
        (
            [0] => The self-study lessons in this section are written and organised according to the levels of the Common European Framework of Reference for languages (CEFR).
        )

    [3] => Array
        (
            [0] =>  
        )

    [4] => Array
        (
            [0] => There are different types of texts and interactive exercises that practise the reading skills you need to do well in your studies, to get ahead at work and to communicate in English in your free time.
        )

)

为了检验上述理论,我运行了这几行 PHP:

        $stop = false;
        $offset = 0;
        while( !$stop ) {
            $stop = (mb_substr($requestText, $offset, 1)!="\n");
            $offset++;
        }
print_r($offset);exit();

结果表明偏移量变量为“1”;这意味着循环只运行了一次并且没有在字符串的开头找到换行符。

问题是:如何根据换行符(包括字符串开头的换行符)(检测和计数)或(分解字符串)?

注意:我正在使用“mb_”系列函数(mb_substr、mb_strlen 等),因为我希望在从右到左的语言中使用 UTF-8 编码的字符串。

** 添加 #1 ** 这是我的 Controller :

class MyController extends BaseController
{
    public function index(Request $request) {

        $input = $request->all();
        if(!isset($input) || empty($input)) {
            return $this->returnJsonResponse($this->badRequestErrorStatus, "Bad Request: Please check the API documentation for its parameters.");
        }

        if(!isset($input["text"]) || empty($input["text"])) {
            return $this->returnJsonResponse($this->badRequestErrorStatus, "Bad Requess: Please provide the text parameter.");
        }

        \Log::info("### New Request Measurements [Chunk Base: " .env("AI_MICROSERVICES_SPELLCHECKER_MAX_REQUEST_TEXT_CHARACTERS_LENGTH"). " Char] ###");

        //--- Capture The Following Block Process Time
        $startMilliseconds = microtime(true)*1000;
        $data['text'] = $this->_chunkText($input["text"]);
        $endMilliseconds = microtime(true)*1000;
        \Log::info(" Chunking Process Time: (( " . ($endMilliseconds - $startMilliseconds) . " )) Milliseconds");
        //---
}

    /**
     * Chunk the passed text according to Business rules.
     *
     * @param String $requestText
     *
     * @return array
     */
    private function _chunkText($requestText) {
        \Log::info("Chunking Process Starts:");

        $stop = false;
        $offset = 0;

        while( !$stop ) {
            $stop = (mb_substr($requestText, $offset, 1)!="\n");
            $offset++;
        }
//        print_r($offset);exit();
}

最佳答案

试试这个:

explode(PHP_EOL, $array)

作为 $array 使用解码后的 JSON 字符串。

关于php - 为什么 PHP 忽略任何字符串开头的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63101244/

相关文章:

javascript - 无法在 Javascript 中对数组进行排序

Ruby % 运算符

ios - 创建一个包含 n 个空格或其他重复字符的字符串

php - 通过 SQL 重置 Moodle 类(class)

php - MySQL 多表关系

php - 如何在没有内存开销的情况下在 PHP curl 中 POST 大量数据?

php - 使用 php 进行 MySQL 搜索查询

ios - 删除 iOS 中的数组元素问题?

arrays - 如何遍历奇数数组并将每个实例加 5?

string - 检查有效域名