1

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.

forgodsakehold
  • 870
  • 10
  • 26

5 Answers5

4

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));
Cid
  • 14,968
  • 4
  • 30
  • 45
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • 2
    The other spaces are included in the second match though...Also if there are multiple space delimited values on one line this will stop after first. – user3783243 Nov 08 '18 at 13:57
  • @user3783243 sure, spaces are included. But according to OP's array and source string there're a lot of spaces that are dissappeared. So, I presume some filtering is used, here some `trim`ing can also be applied. – u_mulder Nov 08 '18 at 14:01
3

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.

user3783243
  • 5,368
  • 5
  • 22
  • 41
1

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"
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
1

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"
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
-2
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 )

)

Fathi
  • 138
  • 2
  • 2
  • 7
  • 1
    Downvoted because 1) Posted code without any explanation, 2) The output isn't what is expected in OP's question – Cid Nov 09 '18 at 13:29
  • what's wrong with you? 1) the code is simple no need to explain. 2) the output is exactly what is needed, read the question please – Fathi Nov 10 '18 at 23:09
  • It's simple, **but** for someone who don't understand regex. This is definitely **not** the expected result. Read again the question and compare against the index 0 of your output. – Cid Nov 11 '18 at 15:11