PHP Mysql avoid registering in the bank in capital letters

4

Good morning,

I have a database, where users enter various news.

I have already talked to all of them, not to put capital letters in the inclusion, because it leaves the project aesthetically ugly.

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

How could I 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 have not done anything like it yet.

Thanks in advance for the help!

    
asked by anonymous 17.12.2015 / 04:03

2 answers

5

I usually use a margin of tolerance. You can do something like this:

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, 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 demo of the IDEONE IDEONE .

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

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

    
17.12.2015 / 04:24
0

One way out is for you to make the title lowercase. To do this use the strtolower() function:

<?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 it capitalizes will be minuscule.

    
17.12.2015 / 04:20