In PHP, str_split() is a very useful function that can split strings into character arrays. This function is especially useful when we need to process the content input by the user at a character level. For example, if we want to check whether user input is legal, we can combine str_split() to achieve this function.
Below we will explain in detail how to use str_split() to split user input and verify whether each character is legal.
Usually, we get user input through HTML forms. We can use $_POST or $_GET to get form data. For simplicity, suppose we have a simple HTML form containing user input:
<form method="post" action="validate.php">
<input type="text" name="user_input" placeholder="Please enter content" />
<button type="submit">submit</button>
</form>
In validate.php , we will process user input.
We can split the string input by the user into a character array through the str_split() function. This way, we can process it character by character.
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_input = $_POST['user_input'];
// use str_split Split the input into a character array
$chars = str_split($user_input);
// Print split character array
print_r($chars);
}
?>
The above code will split the string entered by the user into an array of characters and output it. For example, the input string hello will become ['h', 'e', 'l', 'l', 'o'] .
Suppose we need to check whether each character is a letter (i.e. whether it meets the letter requirements). We can use the ctype_alpha() function to verify whether the character is a letter.
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_input = $_POST['user_input'];
// use str_split Split the input into a character array
$chars = str_split($user_input);
// Verify that each character is legal
foreach ($chars as $char) {
if (!ctype_alpha($char)) {
echo "Illegal characters:$char<br>";
}
}
}
?>
In this example, we loop through each character and use ctype_alpha() to check if it is a letter. If a character is not a letter, the system will prompt an illegal character.
To make the result more user-friendly, we can collect all illegal characters and give prompt information.
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_input = $_POST['user_input'];
// use str_split Split the input into a character array
$chars = str_split($user_input);
$invalid_chars = [];
// Verify that each character is legal
foreach ($chars as $char) {
if (!ctype_alpha($char)) {
$invalid_chars[] = $char;
}
}
// 如果有Illegal characters,Output
if (count($invalid_chars) > 0) {
echo "The following characters are illegal: " . implode(', ', $invalid_chars);
} else {
echo "All characters entered are valid!";
}
}
?>
In this example, all illegal characters are collected into the $invalid_chars array and concatenated into a string through the implode() function, and finally output.
If you need to access certain resources through URLs during the verification process, you can use curl or file_get_contents to verify certain external resources. Assuming you need to verify the data of an external API, we can do this: