Escape sequence in php w3schools

❮ PHP String Reference

Example

Add a backslash in front of the character "W":

<?php
$str = addcslashes("Hello World!","W");
echo($str);
?>

Try it Yourself »


Definition and Usage

The addcslashes() function returns a string with backslashes in front of the specified characters.

Note: The addcslashes() function is case-sensitive.

Note: Be careful using addcslashes() on 0 (NULL), r (carriage return), n (newline), f (form feed), t (tab) and v (vertical tab). In PHP, \0, \r, \n, \t, \f and \v are predefined escape sequences.


Syntax

addcslashes(string,characters)

Parameter Values

ParameterDescription
string Required. Specifies the string to be escaped
characters Required. Specifies the characters or range of characters to be escaped


Technical Details

Return Value:Returns the escaped string
PHP Version:4+

More Examples

Example

Add backslashes to certain characters in a string:

<?php
$str = "Welcome to my humble Homepage!";
echo $str."<br>";
echo addcslashes($str,'m')."<br>";
echo addcslashes($str,'H')."<br>";
?>

Try it Yourself »

Example

Add backslashes to a range of characters in a string:

<?php
$str = "Welcome to my humble Homepage!";
echo $str."<br>";
echo addcslashes($str,'A..Z')."<br>";
echo addcslashes($str,'a..z')."<br>";
echo addcslashes($str,'a..g');
?>

Try it Yourself »


❮ PHP String Reference


In this tutorial you will learn about the PHP Strings and its application with practical example.

PHP Strings variable represents sequences of characters, like “Hello World”. String variable can be any length.PHP String can include escape sequence and are replaced with corresponding character.

PHP Escape Sequence

  • \n is replaced by the newline character
  • \r is replaced by the carriage-return character
  • \t is replaced by the tab character
  • \$ is replaced by the dollar sign itself ($)
  • \” is replaced by a single double-quote (“)
  • \\ is replaced by a single backslash (\)

Examples of string:

$string1="This is a string in double quotes";

$string2="This is a  singly quoted string";

$string0="";// a string with zero characters

Singly quoted strings are treated and will display things almost completely “as is.” Variables and most escape sequences will not be interpreted whereas doubly quoted strings are interpreted before output.

<?php

$name="John";

echo$str1='My name is $name';//Outputs : My name is $name;

echo$str2="My name is $name";//Outputs : 'My name is John;

?>

The String Concatenation Operator in PHP

The concatenation operator (.) is used to bind two string values together.

?php

$txt1="Hello ";

$txt2="World";

echo$txt1." ".$txt2;//Outputs : Hello World

?>

The strlen() function

The strlen() function is used to find the length of a string.

<?php

echostrlen("Hello world");//Outputs: 11

?>

Triming String in PHP

FunctionDescription
trim() Removes whitespace at beginning and end of a string.
ltrim() Removes whitespace at the beginning of a string.
rtrim() Removes whitespace at the end of a string.

Presenting String in PHP

FunctionDescription
htmlentities() Escapes all HTML entities.
nl2br() Inserts a <br /> tag before each newline character in a string.
strtoupper() Converts a string to uppercase.
strtolower() Converts a string to lowercase.
ucfirst() Converts the first character of a string to uppercase.
ucwords() Converts the first character of each word in a string to uppercase.

String Array Conversion

FunctionDescription
explode() Splits a string into an array on a specified character or group of characters.
implode() Converts an array into a string, placing a specified character or group of characters between each array element.
join() Same as implode().

Substring – substr() function in PHP

FunctionDescription
substr(str,pos) Returns the substring from the character in position pos to the end of the string.
substr(str,-len) Returns the substring from len characters from the end of the string to the end of the string.
substr(str,pos,len) Returns a len length substring beginning with the character in position pos.
substr(str,pos,-len) Returns a substring beginning with the character in position pos and chopping off the last len characters of the string.
strstr(haystack,needle,before_needle) If the third argument (before_needle) is false (default), then it returns the part of the haystack from the needle on.

If the third argument (before_needle) is true, then it returns the part of the haystack before the needle.

The needle can be a string or an integer (or a number that can be converted to an integer).

stristr(haystack,needle,before_needle) Same as strstr(), but case insensitive.
strpos(haystack,needle) Finds the position of the first occurrence of a specified needle in a haystack (string).

The needle can be a string or an integer (or a number that can be converted to an integer).

strrpos(haystack,needle) Finds the position of the last occurrence of a specified needle in a haystack (string).

The needle can be a string or an integer (or a number that can be converted to an integer).

str_replace() Replaces all occurrences of one string with another string.

String Comparison – strcmp() function in PHP

FunctionDescription
strcmp() Compares two strings. Returns < 0 if str1 is less than str2, > 0 if str1 is greater than str2, and 0 if they are equal.
strcasecmp() Like strcmp() but case insensitive.

In this tutorial we have learn about the PHP Strings and its application with practical example. I hope you will like this tutorial.

What is the escape sequence in PHP?

An escape sequence tells the program to stop the normal operating procedure and evaluate the following characters differently. In PHP, an escape sequence starts with a backslash \ . Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.

Do I need to escape in PHP?

In practice they don't have to be escaped, except for one case: if the backslash is the last character. 'foo\\' works, but 'foo\' doesn't. I think the non-escaped backslashes should be considered as a syntactic sugar. For consistency you may want to always escape them, but it's up to you.

What is use of Addslashes in PHP?

The addslashes() function returns a string with backslashes in front of predefined characters. The predefined characters are: single quote (')

Which escape sequence can be used in single quoted (') strings in PHP?

Single quoted ¶ The simplest way to specify a string is to enclose it in single quotes (the character ' ). To specify a literal single quote, escape it with a backslash ( \ ). To specify a literal backslash, double it ( \\ ).