PHP爆炸与多个空间

I have strings from a database like this:

$string1 = "1219.56.C38-.C382                 Codex Azcatitlan";
$string2 = "1219.56.C45-.C452                      Codex Cempoallan";

How do I split them up into:

["1219.56.C38-.C382", "Codex Azcatitlan"]
["1219.56.C45-.C452", "Codex Cempoallan"]

Note if I used $stringar1 = explode(" ", $string1) etc. I will get this:

array(3)
(
    [0] => string "1219.56.C38-.C382"
    [1] => string "Codex"
    [2] => string "Azcatitlan"
)

etc.

I need "Codex Azcatitlan"

I do not know in advance how many multiple spaces there are in between the left and right element. However, we can assume that it will always be more than 1 space.

Use preg_split and check for at least 2 whitespace characters.

$string1 = "1219.56.C38-.C382                 Codex Azcatitlan";
$string2 = "1219.56.C45-.C452                      Codex Cempoallan";
print_r(preg_split('/\h{2,}/', $string1));
print_r(preg_split('/\h{2,}/', $string2));

https://3v4l.org/oWbIf

If $strings also should split on newlines change the \h to \s. \h is a horizontal white space (tab or space), \s is any whitespace.

The {} creates a range in regex. A single value inside is the number of allowed characters, a , inside makes a min and max range. 2 is the minimum and no second value means any number of additional matches. This is the same as the + but instead of one match there must be two.

Limit number of parts with third argument of explode() with a combination of array_map() to remove unwanted spaces:

// this means you will have 2 items and all other spaces 
// after first one  will not be used for `explod`ing
$r = array_map('trim', explode(" ", $string1, 2));

You can use a combination of explode() and substr()

$string1 = "1219.56.C38-.C382                 Codex Azcatitlan";

// explode() on spaces
$explode = explode( ' ', trim( $string1 ) ); // apply trim() just in case there are ever leading spaces

$result = array(
    $explode[ 0 ], // give me everything before the first space char
    trim( substr( $string1, strlen( $explode[ 0 ] ) ) ) // give me everything starting from the first space char and apply trim()
);

var_dump( $result );

Output:

array(2) {
  [0]=>
  string(17) "1219.56.C38-.C382"
  [1]=>
  string(16) "Codex Azcatitlan"
}
preg_match_all('~^[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+\.[a-zA-Z0-9]+\s+([a-zA-Z0-9\s]+)~i', '1219.56.C38-.C382                 Codex Azcatitlan', $matches);

print_r($matches);

Array ( [0] => Array ( [0] => 1219.56.C38-.C382 Codex Azcatitlan )

[1] => Array ( [0] => Codex Azcatitlan )

)

You can use a combination of explode(), array_shift(), and implode()

$string1 = "1219.56.C38-.C382                 Codex Azcatitlan";

// explode() on spaces
$explode = explode( ' ', trim( $string1 ) ); // apply trim() just in case there are ever leading spaces

$result = array(
    array_shift( $explode ), // remove the first element from the array and give me it's value
    trim( implode( ' ', $explode ) ) // $explode has it's first element removed so we can just implode it
);

var_dump( $result );

Output:

array(2) {
  [0]=>
  string(17) "1219.56.C38-.C382"
  [1]=>
  string(16) "Codex Azcatitlan"
}