Format city names and ignore words such as "do", "of", "of", "of", "etc

7

I'm working with Webservice whose city names are all deformatted, and I'd like to create a function to treat the names evenly. An example is this:

PORTO DE GALINHAS

I would like it to look like this:

Porto de Galinhas

I would have to give an explode in the string handle all variables at once putting everything in lowercase and right after giving a ucfirst in each word making exceptions of ucfirst in predefined words like of, of, of ...

I know the process but I do not know how to put it in execution.

I tried something:

$string = "PORTO DE GALINHAS";
$array = explode(' ', $string);

foreach ($array as $ar) {
    $dados = strtolower($ar);
    $dados .= " ";

    // Imprime porto de galinhas
    $cidade = trim($dados);
}
    
asked by anonymous 27.12.2015 / 22:02

3 answers

8

An outline that can be easily adapted:

function properCase( $string ) {    
   $ignorar = array( 'do', 'dos', 'da', 'das', 'de' );
   $array = explode(' ', strtolower( $string ) );
   $out = '';
   foreach ($array as $ar) {
      $out .= ( in_array ( $ar, $ignorar ) ? $ar : ucfirst( $ar ) ).' ';
   }
   return trim( $out );
}

echo properCase( 'PORTO DE GALINHAS' ).PHP_EOL;

See working at IDEONE .

  

Important: If you're going to use UTF-8, remember to use mb_strtolower and mb_ucfirst instead of the above functions so that accented letters do not have case error.


Optimizing

Here's an alternative based on the @bigown response:

function properCase( $string ) {
    $ignore = 'de do das da dos';
    $string = ucwords( strtolower( $string ) );
    return str_replace(explode(' ', ucwords( $ignore)), explode(' ', $ignore), $string);
}

echo properCase( 'POUSADA DO BANANAL' );

See working in IDEONE .

    
27.12.2015 / 22:22
6

Alternatively, I would do this:

<?php
function formatarString($str) {
    //torna minuscula e divide string por espaços e tabs
    $vetor = preg_split('#\s#', strtolower($str));

    //Lista de palavras ignoradas
    $ignoreList = array('de', 'as', 'do', 'dos', 'da', 'das');

    $formatada = array();

    foreach ($vetor as $dados) {
        //Ignora strings vazias
        if (empty($dados)) {
             continue;
        }

        if (in_array($dados, $ignoreList)) {
            $formatada[] = $dados;
        } else {
            $formatada[] = ucfirst($dados);
        }
    }

    $output = implode('&nbsp;', $formatada);

    //Limpa referencias
    $vetor = $formatada = NULL;

    return $output;
}

$string = "PORTO DE GALINHAS";
echo formatarString($string);
    
27.12.2015 / 22:26
6

I would do so:

str_replace(array("De ", "Do ", "Dos ", "Da ", "Das "),
     array("de ", "do ", "dos ", "da ", "das "), ucwords(strtolower("PORTO DE GALINHAS")));

See running on ideone .

Documentation for ucwords() .

Documentation for str_replace() .

It can improve the way you treat these things. This is a simplistic way of trying, but that's what asks the question.

Seeing the scattered comments I'll make the version of the function that can customize the exceptions:

function capitalize($string, $search = array("De ", "Do ", "Dos ", "Da ", "Das "), $replace = array("de ", "do ", "dos ", "da ", "das ")) {
    return str_replace($search, $replace, ucwords(strtolower($string)));
}

See running on ideone .

    
27.12.2015 / 22:52