Try it this way:
$string = "ID : 123123 Nome : Elvis Costelo da silva Endereço : Totis bla florianópolis";
$id = preg_split('#(?<!\\\)ID :|Nome :|Endereço :#', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$id = array_map('trim', $id); //Adicionado para eliminar os espaços
var_dump($id);
Prints:
array (size=3)
0 => string '123123' (length=6)
1 => string 'Elvis Costelo da silva' (length=22)
2 => string 'Totis bla florianópolis' (length=24)
Update - Solution without regex: (With New user information).
$string = "Advogado ADVOGADO (OAB: 0000 SC) Código da Publicação 309437294 Disponibilização do Jornal 02/04/2014 Publicação do Jornal 03/04/2014";
$indice = array("Advogado", "Código da Publicação", "Disponibilização do Jornal", "Publicação do Jornal");
for ($i = 0; $i < sizeof($indice); $i++) {
$a = strpos($string, $indice[$i]);
$a_size = strlen($indice[$i]);
if (isset($indice[$i + 1])) {
$b = strpos($string, $indice[$i + 1]);
} else {
$b = strlen($string);
}
$valores[] = substr($string, $a + $a_size, $b - $a - $a_size);
}
$resultado = array_combine($indice, $valores);
$resultado = array_map('trim', $resultado);
var_dump($resultado);
Upshot:
array (size=4)
'Advogado' => string 'ADVOGADO (OAB: 0000 SC)' (length=23)
'Código da Publicação' => string '309437294' (length=9)
'Disponibilização do Jornal' => string '02/04/2014' (length=10)
'Publicação do Jornal' => string '03/04/2014' (length=10)
Question: Where does this data come from? Some Json?
– Marcelo Aymone
@Marceloaymone does it really matter?? This information at the end where I will make the filter will be in a variable.
– Douglas Bernardino
It matters, because if they come from a json format, or something like that, it might be simpler to turn into an array.
– Marcelo Aymone
So, it comes from an expensive text. I do the reading of a standard pdf file, and in that pdf file has that specific information that I need to remove...
– Douglas Bernardino
I get it, so there’s a need to work with the same string
– Marcelo Aymone
Can the value of each item have space? For example, "Name" can come with "Elvis da Silva"?
– danguilherme
It brings me back to this, where I save in a variable. So I wanted to get this information, like Lawyer.. Publication code. .publication.... Lawyer LAWYER (OAB: 0000 SC) Publication Code 309437294 Newspaper Availability 02/04/2014 Newspaper Publication 03/04/2014
– Douglas Bernardino
Apparently, I’m getting it. Rsr
– Marcelo Aymone
@Marceloaymone sorry, it is.. It will be string :D
– Douglas Bernardino
At least it has a type order: it starts from the ID, then it is the Name and then it is the Address? and they are separated by
:
?– user6026
This, will always be the standard indexes with the same name, and are separated by space, because it comes from a table. Ai are separated by space
– Douglas Bernardino
@Douglasbernardino, I was looking here again, and I was thinking of another solution to help you, but for that I would need a more complete example, with the real indexes, I would have like?
– Marcelo Aymone