首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么PHP忽略任何字符串开头的换行符

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

Stack Overflow用户
提问于 2020-07-26 14:08:40
回答 2查看 158关注 0票数 3

我期待从请求,将来到我的Laravel微服务有一个用户输入文本。

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

代码应根据换行符“拆分”该文本,并按其自身处理每一段。

例如:请求中有此字符串:

代码语言:javascript
运行
复制
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."
}

我希望这个阵列:

代码语言:javascript
运行
复制
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.
        )

)

但不幸的是,我有这样的阵列:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
        $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 **这是我的控制器:

代码语言:javascript
运行
复制
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();
}
EN

回答 2

Stack Overflow用户

发布于 2020-07-26 16:58:21

试试这个:

代码语言:javascript
运行
复制
explode(PHP_EOL, $array)

$array使用您解码的JSON字符串时。

票数 0
EN

Stack Overflow用户

发布于 2020-07-27 06:16:57

非常感谢user3532758,我通过注释删除了App\Http\Kernel类中的"TrimString“中间件。

看看这个:disable web middleware for specific routes in laravel 5.2

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63101244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档