Detect no characters in a string

Asked

Viewed 2,537 times

1

How do I make to detect the existence noncharacters like this in a string? �*�v��R�<�

  • has the code and if you have post in the question to help in the answer?

  • is an Encryption/decryption code based on various functions and keys.. However when decryption does not work it returns these characters, I want that when this happens he repeats the function until he returns the decrypted string and for me to do this I need him to make a check to see if the resulting string has these strange characters..

  • Post the code if it doesn’t get hard to help

  • If you’re having a way out of that, it’s because, in principle, there’s something wrong with the code. For perfect encryption/decryption the initial string must equal the final one after encryption and decryption, respectively. What you want is a kind of a gambiarra, which may end up screwing up in the future.

  • What exit do you want?

3 answers

3


"Regular" ASCII characters span from hexadecimal 20 at the 7F:

Imagem da tabela ASCII

Image credits for Wikimedia Commons.

Therefore, a practical method is to check that all characters in the string are within this ASCII interval, where for this purpose we can make use of a regular expression:

Example where characters are outside the list:

$string = "�*�v��R�<�";

if (preg_match('/[^\x20-\x7f]/', $string)) {
  echo "Correu mal, chamar função novamente!".PHP_EOL;   // vai entrar aqui
}
else {
  echo "Todos os caracteres são legíveis!".PHP_EOL;
}

Example where characters are inside the list:

$string = "HGKER%(()W(/T%&)WREGDG";

if (preg_match('/[^\x20-\x7f]/', $string)) {
  echo "Correu mal, chamar função novamente!".PHP_EOL;
}
else {
  echo "Todos os caracteres são legíveis!".PHP_EOL;      // vai entrar aqui
}

Examples working on Ideone.

2

To detect characters in a string vc can use the function preg_match:

$numero = '/(1|2|345|67|7890)/';
$frase = 'abcdefgh ijklmno pqrstu vxz';

echo preg_match($palavra, $frase);

But these characters that you want to identify, have to do with the charset of your bank or your code, probably modifying one of the two these characters will no longer appear:

You can set the charset in HTML:

>meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

You can set the charset in PHP:

header('Content-Type: text/html; charset=UTF-8');

And you can set the charset in Mysql:

$conn = mysql_connect("localhost", "root", ""); //("servidor", "usuário", "senha" definidos no banco de dados)

mysql_query("SET NAMES 'utf8'");
mysql_query("SET character_set_connection=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_results=utf8")

And if you are using PDO:

$conn = new \PDO('mysql:host=localhost;dbname=bancoTeste', //dsn
                 'root', //user
                 '123456', //senha
                 array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") // opções PDO
              );

2

Browser other questions tagged

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