Cepheus
Cepheus

Reputation: 4893

How can I split text with unknown amount of space between strings?

The string in question is of this nature:

"France                        1.27"

There is no way to know the amount of space between them because it changes but there is always a space. How can I access each string?

Upvotes: 0

Views: 57

Answers (2)

Spoody
Spoody

Reputation: 2882

Well I can think of two other ways, first one is to remove multiple spaces:

$string = "France                        1.27";
$string = preg_replace('/\s+/', ' ', $string); // Remove double spaces

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

Or exploding it as it is and removing empty values:

$string = "France                        1.27";
$string = explode(" ", $string);
$string = array_filter($string); // Remove empty elements
$string = array_values($string); // Re-index the array, array_filter will mess up the indexes

Upvotes: 2

wizebin
wizebin

Reputation: 730

You can use a regular expression

http://php.net/manual/en/function.preg-split.php

Something like

$keywords = preg_split("/\s+/", "hello     world    hi");
print_r($keywords)

Upvotes: 2

Related Questions