Thursday, 25 September 2014

addcslashes in PHP

PHP addcslashes() function is utilized to includes backslashes in front of specified character in a string.

Syntax:

addcslashes(string,characters)
 
Parameters Description: 
string : Required. Define the input string
characters : Define Sequence of characters to be affected by PHP addcslashes().
Note : The PHP addcslashes() function is case-sensitive. 

Example:

<?php 
echo "<strong>Adding backslashes to certain characters in a string :</strong>";
$str = "Hello, have a nice Day.";
echo $str."<br />";
echo addcslashes($str,'a')."<br />";
echo addcslashes($str,'D')."<br />";
echo "<strong>Adding backslashes to a range of characters in a string :</strong>";
$str = "Hello, have a nice Day.";
echo $str."<br />";
echo addcslashes($str,'A..Z')."<br />";
echo addcslashes($str,'a..z')."<br />";
echo addcslashes($str,'a..h')."<br />";
?>

O/P:

Adding backslashes to certain characters in a string :
Hello, have a nice Day.
Hello, h\ave \a nice D\ay.
Hello, have a nice \Day.
Adding backslashes to a range of characters in a string :
Hello, have a nice Day.
\Hello, have a nice \Day.
H\e\l\l\o, \h\a\v\e \a \n\i\c\e D\a\y.
H\ello, \h\av\e \a ni\c\e D\ay.

0 comments:

Post a Comment