PHP Mysql avoid database registration in uppercase letters

Asked

Viewed 758 times

4

Good morning,

I have a database, where users insert various news.

I’ve talked to all of them, not to put capital letters in the inclusion, because it makes the project aesthetically ugly.

Is there a way to issue an alert when registering? For example: "Your title is in capital letters, please correct".

How could generate this alert using the code below?

<?php
require 'conexao1.php';
header ("Content-type: text/html; charset=utf-8");

$titulo                 = addslashes ($_POST['titulo']);

$sql = "INSERT INTO noticias (titulo) VALUES ('$titulo')";

mysql_query($sql) or die (mysql_error());

if ($sql) {
echo "<script> alert('Título inserido com sucesso!');
}

?>

I’ve been looking for some information, but I confess I haven’t done anything like this yet.

Thanks in advance for the help!

  • If you are going to display a Javascript warning, if you are going to save the submitted title in uppercase, convert it into minusculas in php and save without warning the user

  • I prefer the warning, because if the user puts a proper name for example, it will be all lowercase, and incorrect in the view. " paulo" instead of "Paulo" or "brasil" instead of "Brasil".

  • It really makes sense, in this case.

2 answers

5


I usually use a margin of tolerance. You can do something about that:

function porcentagem_maiusculas( $string ) {
   $count = 0;
   $len = mb_strlen( $string );

   for( $i = $len - 1; $i >=0 ; --$i ) {
      $char = mb_substr( $string, $i, 1 );
      $count += ( mb_strtolower( $char ) == $char ? 0 : 1 );
   }
   return 100 * $count / $len;
}

Then, to use the alert, just something like:

if( porcentagem_maiusculas( $titulo ) > 40 ) {
   echo 'Você usou maiúsculas demais no título. Escreva direitinho, senão não aceitamos. Por favor, verifique!';
} else {
   $sql = "INSERT INTO noticias (titulo) VALUES ('$titulo')";
   mysql_query($sql) or die ( mysql_error() );
   echo "<script>alert('Título inserido com sucesso!')</script>";
}

See a demonstration of the function on IDEONE.

The recommendation is to give a reasonable tolerance (initially 40 or 50%), so that the person can use acronyms correctly in capital letters in the title, but at the same time leave the CAPS LOCK thimbles in the axis.

In my applications I use a approach a little more complex, changing the tolerance according to the size of the string. More tolerant short strings, and as it increases, tolerance drops, but maybe it’s a bit of an exaggeration.

0

An output is you make the title lower case. To do this use the function strtolower():

<?php
require 'conexao1.php';
header ("Content-type: text/html; charset=utf-8");

$titulo                 = addslashes(strtolower($_POST['titulo']));

$sql = "INSERT INTO noticias (titulo) VALUES ('$titulo')";

mysql_query($sql) or die (mysql_error());

if ($sql) {
echo "<script> alert('Título inserido com sucesso!');
}

?>

So any character he uppercase will be lowercase.

  • The idea is great, @Alissonacioli, but everything will be in lowercase, including first names, will not look good in the view. I prefer to send the warning, so "teaching" also to the user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.