String Functions in PHP: PART 1

Introduction

A string is a set of characters containing a vast number of useful functions that enable you to, for example:

  • Concatenate two or more strings.
  • Compare two strings.
  • Copy a string.
  • Determine the number of characters in a string.

A string also contains too many functions. I explain some of them here.

PHP addcslashes() Function

The PHP string addcslashes() function returns a string with backslashes before the specified characters. This function is useful for inserting a string into a database.

Syntax

addcslashes(string, characters)

Parameter in addcslashes Function

The parameters of the addcslashes function are:

Parameter Description
string It specifies the string to check.
characters It specifies a list of characters to be escaped.

Example

An example of the
addcslashes function is:

<?
php
$
str="Hi Everone";
echo
addcslashes($str,"E")."</br>";
?>


Output

 addcslashes-string-function-in-php.jpg

PHP addslashes() Function

The PHP string addslashes() Function returns a string with backslashes in front of predefined characters. This function is useful for inserting a string into a database.

Syntax

addslashes(string)

Parameter in addslashes  Function

The parameter of the addcslashes function is:

Parameter Description
string It specifies the string to check.

Example

An example of the
addslashes function is:

<?
php
echo
addslashes('Hi,"Everyone"');
?>
 

Output

addslashes-string-function-in-php.jpg
 

PHP bin2hex() Function

The PHP string bin2hex function is used to convert binary data into hexadecimal representation.


Syntax

bin2hex(string)

Parameter in bin2hex Function

The parameter of the addcslashes function is:

Parameter Description
string It specifies the string to be converted.


Example

An example of the
bin2hex function is:

<?
php
$
str='Hi, Everyone';
echo
$str."<br/>";
echo
"And its hexadecimal value is: ";
echo
bin2hex($str);
?>


Output

bin2hex-string-function-in-php.jpg


Similar Articles