preg_split to separate words, but ignoring some

0

I need a regular expression that divides a string, more specifically a complete name of a person, and turns it into an array of words.

$string = "Wallace de Souza Vizerra";

$array = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);

['Wallace', 'de', 'Souza', 'Vizerra']

However, I need the result to be as follows when there are occurrences de , da , do , das and dos .

['Wallace', 'de Souza', 'Vizerra']

Can someone with regular expression handle help me and explain to me how the regular expression used in the response would work?

If there is any way, I would also like to remove the first word through the regular expression as well.

That is

$string = "Wallace de Souza Vizerra"

should return

['de Souza', 'Vizerra']
    
asked by anonymous 03.07.2015 / 22:06

1 answer

2

Success, but there is no way to remove the first item from array resulting from preg_split using the function itself and not even another function inline , so array_shift was used (could also use unset($array[0]) ).

separa_palavras.php

<?php

$nome_completo = 'Wallace de Souza Vizerra dos Santos';

$resultado = preg_split('/(?<!de|da|do|dos|das)[\s]/i', $nome_completo);
$nome_removido = array_shift($resultado);
reset($resultado); // re-ordena chaves

var_export($resultado);

Output

array (
  0 => 'de Souza',
  1 => 'Vizerra',
  2 => 'dos Santos',
)
    
04.07.2015 / 03:54