PHP addcslashes

 The PHP addcslashes function can add a backslash (\) in front of the specified character to make it a escape character. The string filtering before data processing is quite easy to use. addcslashes is similar to addslashes , but addcslashes is more flexible. Because you can decide which characters to add a backslash before, unlike addslashes which can only handle specific characters. In addition, the addcslashes function also has a special function, which can set all characters in a range of English letters as escape characters, and the case of English letters can also be distinguished.


Basic syntax of PHP addcslashes function

String addcslashes( $string, $charlist)


In the grammar, there are two parameters in the parentheses of the addcslashes function. The first parameter $string is the string to be converted, which is required. The second parameter $charlist is used to set which characters to skip, that is A backslash (\) will be added in front of these set characters, and unique character symbols or English alphabetical sequence sections can be set, with different capitalization.

PHP addcslashes function example
<?php
$new_string = "Welcome to wibibi.It was a very nice day.";
echo $new_string.'<br>';
echo addcslashes($new_string,'W').'<br>';
echo addcslashes( $new_string,'w').'<br>';
echo addcslashes($new_string,'A..Z').'<br>';
echo addcslashes($new_string,'a..z').'< br>';
echo addcslashes($new_string,'A..z').'<br>';
?>
The above output
Welcome to wibibi.It was a very nice day.
\Welcome to wibibi.It was a very nice day.
Welcome to \wibibi.It \was a very nice day.
\Welcome to wibibi.\It was a very nice day.
W \e\l\c\o\m\e \t\o \w\i\b\i\b\iI\t \w\a\s \a \v\e\r\y \n\i \c\e \d\a\y.
\W\e\l\c\o\m\e \t\o \w\i\b\i\b\i.\I\t \w\a \s \a \v\e\r\y \n\i\c\e \d\a\y.
The results of the second and third output of the example mainly show that the addcslashes function judges the case of English letters. Because the ASCII Code of the case of English letters is not the same, addcslashes will automatically distinguish it, and then the output of the next three times is presented The addcslashes function judges the order of English letters. If A..Z is uppercase, all uppercase letters in the string $new_string will become escape characters. On the contrary, if a..z is lowercase, then escape characters The lowercase characters in the string, if A..z is capitalized and then lowercased in this way, all English letters in $new_string, regardless of uppercase or lowercase, will become escape characters, and other symbols such as blanks or commas will not have any effect.

Post a Comment

0 Comments