3

I am expecting from the request that will come to my Laravel microservice to have a user input text.

The case that I have is that the user enters multiple newline characters at the beginning of the paragraph they write.

The code should "split" that text according to the newline and process each paragraph by its own.

For example: I have this string in the request:

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."
}

I am expecting to have this Array:

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

)

But unfortunately, I have this Array:

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

)

To test the above theory, I have run this few PHP lines:

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

The result says that the offset variable is "1"; which means that the loop run only one time and did not find the newlines at the beginning of the string.

The question is: How I can (detect and count) or (explode the string) according to the newlines including the ones at the beginning of the string?

Note: I am using the "mb_" family functions (mb_substr, mb_strlen, ... etc) because I am expecting UTF-8 encoded strings that used in Right-To-left languages.

** Addition #1 ** This is my 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();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

Try this:

explode(PHP_EOL, $array)

As $array use your decoded JSON string.

GreenPepper
  • 138
  • 2
  • 11
0

Thank you so much user3532758 I have disabled the "TrimString" Middleware in App\Http\Kernel class, by comment it out.

check this out: disable web middleware for specific routes in laravel 5.2