PHP Strings, Escaping string and formating string

Strings wear many hats in PHP—far from being relegated to mere collections of textual characters,  they can be used to store binary data of any kind—as well as text encoded in a way that PHP does not understand natively, but that one of its extensions can manipulate directly.
String Basics
Strings can be defined using one of several methods. Most commonly, you will encapsulate them in single quotes or double quotes. Unlike some other languages, these two methods behave quite differently: single quotes represent "simple strings," where almost all characters are used literally. Double quotes, on the other hand, encapsulate "complex strings" that allow for special escape sequences (for example, to insert special characters) and for variable substitution, which makes it possible to embed the value of a variable directly in a string, without the need for any special
operator.
Escape sequences are sometimes called control characters and take the form of a backslash (\) followed by one or more characters. Perhaps the most common escape sequence is the newline character \n. In the following example, we use hex and octal notation to display an asterisk:
<?php
echo "\x2a";
echo "\052";
?>
Variable Interpolation
Variables can be embedded directly inside a double-quote string by simply typing their name. For example:
<?php
$who = "World";
echo "Hello $who\n"; // Shows "Hello World" followed by a newline
echo 'Hello $who\n'; // Shows "Hello $who\n"
?>
Clearly, this “simple” syntax won’t work in those situations in which the name of the variable you want to interpolated is positioned in such a way inside the string that the parser wouldn’t be able to parse its name in the way you intend it to. In these cases, you can encapsulate the variable’s name in braces:
<?php
$me = 'Davey';
$names = array('Smith', 'Jones', 'Jackson');
echo "There cannot be more than two {$me}s!";
echo "Citation: {$names[1]}[1987]";
?>
In the first example above, the braces help us append a hard-coded letter “s” to the value of  $me—without them, the parserwould be looking for the variable $mes, which, obviously, does not exist. In the second example, if the braces were not available, the parser would interpret our  input as $names[1][1987], which is clearly not what we intended.
The Heredoc Syntax
A third syntax, called heredoc, can be used to declare complex strings—in general, the functionality it provides is similar to double quotes, with the exception that, because heredoc uses a special set of tokens to encapsulate the string, it’s easier to declare strings that include many double-quote characters.
A heredoc string is delimited by the special operator <<< followed by an identifier.
Youmust then close the string using the same identifier, optionally followed by a semicolon, placed at the very beginning of its own line (that is, it should not be preceded by whitespace). Heredoc identifiers must follow the same rules are variable naming (explained in the PHP Basics chapter), and are similarly case-sensitive.
The heredoc syntax behaves like double quotes in every way, meaning that variables and escape sequences are interpolated:
<?php
$who = "World";
echo <<<TEXT
So I said, "Hello $who"
TEXT;
?>
The above code will output So I said, “Hello World”. Note how the newline characters right after the opening token and right before the closing token are ignored. Heredoc strings can be used in almost all situations in which a string is an appropriate value. The only exception is the declaration of a class property (explained in the Object Oriented ProgrammingWith PHP chapter), where their use will result in a parser error:
<?php
class Hello {
public $greeting = <<<EOT
Hello World
EOT;
}
?>
Escaping Literal Values
All three string-definition syntax feature a set of several characters that require escaping in order to be interpreted as literals.
When using single-quote strings, single quote characters can be escaped using a backslash:
<?php
echo 'This is \'my\' string';
?>
A similar set of escaping rules apply to double-quote strings, where double quote
characters and dollar sign can also be escaped by prefixing them with a backslash:
<?php
$a = 10;
echo "The value of \$a is \"$a\".";
?>
Backslashes themselves can be escaped in both cases using the same technique:
<?php
echo "Here's an escaped backslash: - \ -";
?>
Note that you cannot escape a brace—therefore, if you need the literal string {$ to be printed out,
you need to escape the dollar sign in order to prevent the parser from interpreting the sequence as
an attempt to interpolate a variable:
<?php
echo "Here's a literal brace + dollar sign: {\$";
?>
Heredoc strings provide the same escaping mechanisms as double-quote strings, with the exception that you do not need to escape double quote characters, since they have no semantic value.
Determining the Length of a String
The strlen() function is used to determine the length, in bytes, of a string. Note that strlen(), like most string functions, is binary-safe. This means that all characters in the string are counted, regardless of their value. (In some languages (notably C), some functions are designed to work with “zero-terminated” strings, where the NUL character is used to signal the end of a string. This causes problems when dealing with binary objects, since bytes with a value of zero are quite common; luckily, most PHP functions are capable of handling binary data without any problem.)
Transforming a String
The strtr() function can be used to translate certain characters of a string into other characters—it is often used as an aid in the practice knownas transliteration to transformcertain accented characters that cannot appear, for example, in URLs or e-mail address into the equivalent unaccented versions:
<?php
// Single character version
echo strstr('abc', 'a', '1'); // Outputs 1bc
// Multiple-character version
$subst = array(
'1' => 'one',
'2' => 'two',
);
echo strtr('123', $subst); // Outputs onetwo3
?>
Using Strings as Arrays
You can access the individual characters of a string as if they were members of an array. For example:
<?php
$string = 'abcdef';
echo $string[1]; // Outputs 'b'
?>
This approach can be very handy when you need to scan a string one character at a time:
<?php
$s = 'abcdef';
for($i = 0; $i < strlen($s); $i++) {
if($s[$i] > 'c'){
echo $s[$i];
}
?>

1 comment:

  1. Good post. I learn something totally new and challenging on blogs I stumble upon on a daily basis. It will always be interesting to read articles from other authors and practice something from their websites..
    Dot Net Training Institute in Chennai
    Software Testing Course in Chennai
    Core Java Training in Chennai
    PHP Certification in Chennai

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...