0

I have a string that looks like this "thing aaaa" and I'm using explode() to split the string into an array containing all the words that are separated by space. I execute something like this explode (" ", $string) .

I'm not sure why but the result is : ["thing","","","","aaaa"]; Does anyone have an idea why I get the three empty arrays in there ?

EDIT : This is the function that I'm using that in :

public function query_databases() {

        $arguments_count = func_num_args();
        $status = [];
        $results = [];
        $split =[];

        if ($arguments_count > 0) {

            $arguments = func_get_args();

            $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arguments));

            foreach ($iterator as $key => $value) {
                array_push($results, trim($value));
            }
            unset($value);

            $filtered = $this->array_unique_values($results, "String");

            foreach ($filtered as $key => $string) {

                if (preg_match('/\s/',$string)) {
                    array_push($split, preg_split("/\s/", $string));
                } else {
                    array_push($split, $string);
                }

            }
            unset($string);

            echo "Terms : ".json_encode($split)."<br>";

            foreach ($filtered as $database) {

                echo "Terms : ".json_encode()."<br>";
                $_action = $this->get_database($database);
                echo "Action : ".json_encode($_action)."<br>";
            }
            unset($database);


        } else {
            return "[ Databases | Query Databases [ Missing Arguments ] ]";
        }

    }

It might be something else that messes up the result ?!

8
  • 2
    Most browsers ignore whitespace unless &nbsp; was used -- What is the content of your string? Use var_dump(). Commented Jan 28, 2013 at 18:13
  • 5
    Must be something funny with the input string as it works for me -> codepad.org/pXoZ3EW0 Commented Jan 28, 2013 at 18:13
  • 3
    can we see the entire code? how youre storing it, echoing it, etc Commented Jan 28, 2013 at 18:14
  • Seems there are some invisible characters (or even more whitespace) between your words. codepad.org/g46oM0hd Commented Jan 28, 2013 at 18:15
  • Are you sure the string only has a single space between the words? The result you report is what I'd expect with 4 spaces between the words. Commented Jan 28, 2013 at 18:17

3 Answers 3

1

If you are looking to create an array by spaces, you might want to consider preg_split:

preg_split("/\s+/","thing aaaa");

which gives you array ("thing","aaaa");

Taken from here.

Sign up to request clarification or add additional context in comments.

Comments

0

try this:

$str = str_replace(" ", ",", $string);
explode(",",$str);

This way you can see if it is just the whitespace giving you the problem if you output 4 commas, it's because you have 4 whitespaces.

Comments

0

As @Barmar said, my trim() just removes all the space before and after the words, so that is why I had more values in the array than I should have had.

I found that this little snippet : preg_replace( '/\s+/', ' ', $value ) ; replacing my trim() would fix it :)

Comments

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.