terça-feira, 4 de janeiro de 2011

Strings com a letras em maiúsculo quando necessário.

Exemplos:

$palavra=strtolower($palavra); //converte todas em minuscula
$palavra=ucfirst($palavra); //converte só a 1ª em maiucula

Script que pode colocar palavras em uma string em maiúsculas quando necessário...

<?
$nome = "naylor garcia dos santos";
$nome_mae = "vitoria GaRCia";
$endereco = "kljfkd";
$bairro = "CentRo";
for ($i=0; $i < 1; $i++) {
$res_fin = "";
if ($i == 0) $string[$i]=ucfirst($nome);
if ($i == 1) $string[$i]=ucfirst($nome_mae);
if ($i == 2) $string[$i]=ucfirst($endereco);
if ($i == 3) $string[$i]=ucfirst($bairro);
$string[$i] = strtolower($string[$i]);
for ($j=0; $j <= strlen($string[$i]); $j++) {
$res_str = substr($string[$i], $j, 1);
if ($res_str == " ") {
$pre = substr($string[$i], $j+3, 1);
echo "1 $pre<br><br>";
if ($pre != " ") $pre = substr($string[$i], $j+4, 1);
$pos = substr($string[$i], $j+1, 3);
echo "2 Pre:$pre<br>Pos:$pos<br><br>";
if ((($pre == " ") && ($pos == "de ")) || (($pre == " ") && ($pos == "do ")) || (($pre == " ") && ($pos == "das")) || (($pre == " ") && ($pos == "dos")) || (($pre == " ") && ($pos == "das")) || (($pre == " ") && ($pos == "da "))) {
$res_fin = "$res_fin $res_par";
} else {
$res_par = ucfirst(substr($string[$i], $j+1, 1));
$res_fin = "$res_fin $res_par";
$res_par = "";
$j = $j + 1;
}
} else {
$res_fin = "$res_fin$res_str";
}
}
if ($i == 0) $nome=ucfirst($res_fin);
if ($i == 1) $nome_mae=ucfirst($res_fin);
if ($i == 2) $endereco=ucfirst($res_fin);
if ($i == 3) $bairro=ucfirst($res_fin);
}
echo "<br>Nome: $nome";
echo "<br>Nome Mãe: $nome_mae";
echo "<br>Endereço: $endereco";
echo "<br>Bairro: $bairro";
?>