3

I am trying to create an array from input in a textarea.

I have a file named with a textarea.The textarea might contain values like(exactly the way it looks):

CREATE this.

DO that.

STOP it.

Basically,I want to use PHP to :

Create an array out of the values given by the textarea for example,the array madefrom th values of the textarea above is meant to be :

Array
(
    [0] => create this
    [1] => do this
    [2] => Stop it

)

I'v e tried the following code:

 <?php 

    $wholecode=$_POST['code'];

     $code=explode('.',trim(strtolower($wholecode)));//convert code to array


    $words=explode(' ', $code);


print_r($code);

I get

Array
(
    [0] => create this
    [1] => 

do this
    [2] => 

stop it
    [3] => 
)

As it clearly shows,that is not what I want.Please help

3
  • 1
    $code=explode('.'.PHP_EOL,trim(strtolower($wholecode))); try with this part ? Commented Dec 13, 2018 at 14:52
  • You are currently splitting this at the dots only, which of course leaves the line break characters in place. Commented Dec 13, 2018 at 14:52
  • @MorganFreeFarm '.PHP_EOL' is the literal text .PHP_EOL ... constants do not get replaced in single or double quoted strings, you need to use string concatenation in this place: '.'.PHP_EOL Commented Dec 13, 2018 at 14:54

2 Answers 2

4

You just need to tidy up the array contents after creating it. You have things like new lines and potentially other whitespace around the content.

This uses array_map() to trim() each entry in the array. Then uses array_filter() to remove any empty elements (calling it with no callback will do this).

$wholecode=$_POST['code'];

$code=explode('.',trim(strtolower($wholecode)));//convert code to array
$code=array_map("trim", $code );
$code = array_filter($code);
print_r($code);
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to make all that to a single line?
@bestboy $array = array_filter(array_map("trim", explode('.',trim(strtolower($_POST['code'])))));
2

Here's a direct approach using a simple regex pattern to explode on dots followed by zero or more whitespace characters. No mopping up after splitting.

Code (Demo)

$_POST['code'] = 'CREATE this.

DO that.

STOP it.';

var_export(preg_split('~\.\s*~', strtolower($_POST['code']), -1, PREG_SPLIT_NO_EMPTY));

Output:

array (
  0 => 'create this',
  1 => 'do that',
  2 => 'stop it',
)

As a variable...

$array = preg_split('~\.\s*~', strtolower($_POST['code']), -1, PREG_SPLIT_NO_EMPTY)

Explanation transferred from my comment:

Pattern Demo: https://regex101.com/r/jygaQ1/1

There are 3 dots in your sample data. The first two have whitespaces characters that immediately follow. The final dot has no trailing whitespace characters.

The \s* means "match zero or more whitespace characters.

-1 means perform unlimited explosions.

PREG_SPLIT_NO_EMPTY means that on the final explosion (the last dot) there will be an empty element generated, but preg_split() will disregard it in the output array.

8 Comments

Please can you explain it to me..ur answer
regex101.com/r/jygaQ1/2 This is what we call a "rabbithole" You can keep extending the requirements, but then I'll keep refining the pattern, but I'll end up wasting a lot of my time doing lots of edits. It is best that you fully represent your problem and the multitude of ways that your input text can vary when you first post your question.
Also,if there is no string at all..it prints Array()
...rabbit hole. What were you hoping for if not an empty array? Our comments are too many, we must tidy up this page.
Done that.I'm trying to build something like a programming language with it.That is why I was asking all thesese questions.Please dont be mad
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.