String is the most common type of variable in programming, no matter the language. Every type of referential textual uses this format to be handled or stored, so all programming languages provide many resources to work with them.
In PHP, the commands for working with String are diverse, check out some following.
Character Count strlen ()
This function is used to count the number of characters in a string, as shown below:
<?php
$str1 = “”;
$str2 = “scriptcase”;
echo (“String 1: ” . strlen($str1) . ” characters .<BR>”);
echo (“String 2: ” . strlen($str2) . ” characters .<BR>”);
?>
Uppercase strtoupper() and Lowercase strtolower()
To convert all the letters in a string to uppercase just use the reference strtoupper() to lowercase using strtolower(). Both convert all the letters, but not work as opposing case find a string of characters that is already in uppercase or lowercase.
<?php
$cd = “abc”;
$text1 = “It is a Test.”;
$cdMa = strtoupper($cd);
$texto1Me = strtolower($text1);
?>
CTYPE
Since version 4.2.0 of PHP for Windows, the library CTYPE was added for use. From the verification method chosen if returns a value of true or false and that there is a greater control over the strings of the project. Check out the methods below:
- ctype_alnum — Verifica se os caracteres são alfanuméricos;
- ctype_alpha — Verifica se os caracteres são apenas alfabéticos;
- ctype_cntrl — Verifica se os caracteres são de controle;
- ctype_digit — Verifica se os caracteres são apenas numéricos;
- ctype_graph — Verifica se os caracteres são imprimíveis. Isso deduz que sejam caracteres possuam alguma forma que se possa visualizar, portanto, espaços em branco não são considerados imprimíveis por esta função;
- ctype_lower — Verifica se os caracteres estão minúsculos;
- ctype_print — Verifica se os caracteres são imprimíveis. Isso exclui quebras de linhas e outros controles de fluxo de texto;
- ctype_punct — Verifica se é um caractere imprimível que não é um espaço em branco, letra ou numéro, como por exemplo, dois-pontos (:), ponto de interrogação (?), vírgula (,), …;
- ctype_space — Verifica se os caracteres são espaços em branco;
- ctype_upper — Verifica se os caracteres são maiúsculos;
- ctype_xdigit — Verifica se os caracteres representam um dígito hexadecimal. Ou seja, números e letras de A a F.
In PHP, different from most programming languages, there is no need to declare the type of the variable, so the controls and checks the data type must be well considered and implemented. In the case of the string, there are several ways to validate and help the programmer to have much more control over this item.
I hope you enjoyed these tips. Follow our blog and stay on top of all the news.
You might also like…