【问题标题】:Why PHP ignores the newline characters in the beginning of any string为什么 PHP 会忽略任何字符串开头的换行符
【发布时间】:2020-11-15 22:52:32
【问题描述】:

我期望来自我的 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 ** 这是我的控制器:

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

【问题讨论】:

  • @user3783243 与问题上发布的证据无关。
  • 你能显示实际执行上述变量的代码,以输出意外的字符串吗?那里可能有一个trim() 函数,.,,..
  • @user3783243 不,我正在使用邮递员,我的错我应该在问题中澄清这一点。
  • Laravel 有一个 TrimString 中间件。它将从输入中修剪空格和换行符。要禁用字段的该行为,您需要将字段名称添加到该中间件文件中的 except 数组中。
  • @user3532758 非常感谢!我已经禁用了 App\Http/Kernel 类中的“TrimString”中间件,通过注释掉它。

标签: php arrays string laravel newline


【解决方案1】:

试试这个:

explode(PHP_EOL, $array)

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

【讨论】:

    【解决方案2】:

    非常感谢 user3532758 我已经禁用了 App\Http\Kernel 类中的“TrimString”中间件,将其注释掉。

    检查一下: disable web middleware for specific routes in laravel 5.2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      相关资源
      最近更新 更多