@
@
@ksider2018-06-10 00:10:38
PHP
@ksider, 2018-06-10 00:10:38

How to split a string into an array?

How to split a string into an array
$name = "Ньютон И.И. (Тринити колледж, Кембридж)";

[0] => Ньютон И.И. 
[1] => Тринити колледж, Кембридж

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2018-06-10
_

You can use a regular expression (since such a tag is in question). Literally "anything, then a space-with-parenthesis, then everything up to the parenthesis, parenthesis." Throw out the zero element from the resulting array, which contains the entire string:

preg_match('/^(.+)\ \((.+)\)/', $name, $matches);
array_shift($matches);
$matches; /*Array
(
    [0] => Ньютон И.И.
    [1] => Тринити коледж, Кембридж
)*/

You can just cut off the last character, and then split it into an array, using a space-with-open-bracket as a separator : )
without regular expressions
$name = "Ньютон И.И. (Тринити коледж, Кембридж)";
  $name = substr($name, 0, -1);
  $result = explode(' (', $name);
  print_r($result);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question