PHP/String/String
Содержание
- 1 An Escape
- 2 Check string password
- 3 Combine an expression with a string
- 4 Comparing strings
- 5 Comparing strings with the equality operator
- 6 curly offset syntax
- 7 Escape Characters in PHP
- 8 Escape Characters That Act As Anchors
- 9 Escape sequences and their meanings (continued)
- 10 Getting an individual byte in a string
- 11 Indexing Strings
- 12 PHP 5 Substring Functions
- 13 Processing each byte in a string
- 14 Some string examples
- 15 Supported String Delimiters
- 16 Using Escaped Characters
- 17 Using {x} notation with strings to read or write individual characters
An Escape
\ indicates the escape character.
Check string password
<?php
$GoodPassword = "asdfg";
$password = "asdf";
if ($password == $GoodPassword){
print "<b>Password verified!</b>\n";
}
?>
Combine an expression with a string
<?
$centimeters = 212;
print "the width is ".($centimeters/100)." meters";
?>
Comparing strings
<?
$word = "baa";
if ($word < "baa") {
print "Your word probably starts with "A".";
}
if ($word >= "zoo") {
print "Your word could be zoo or zymurgy, but not zone.";
}
?>
Comparing strings with the equality operator
<?
if ("p@demo.ru" == "p@demo.ru") {
print "Welcome, Mr. President.";
}
?>
curly offset syntax
<?php
$thing = "php";
echo $thing;
echo "<br />";
echo $thing{0};
echo $thing{1};
echo $thing{2};
?>
Escape Characters in PHP
Escape String Resulting Character
\n Linefeed character
\r Carriage return character
\t Horizontal escape character escape character escape character escape character escape character tab character
\\ The backslash character
\$ The $ character
\" The single-quote character
\" The double-quote character
\### ASCII character (octal)
\x## ASCII character (hexadecimal)
Escape Characters That Act As Anchors
Character Matches
\A Beginning of string
\b Word boundary
\B Not a word boundary
\Z End of string (matches before final newline or at end of string)
\z End of string (matches only at very end of string)
Escape sequences and their meanings (continued)
\" Print the next character as a double quote rather than treating it as a string terminator
\" Print the next character as a single quote rather than treating it as a string terminator
\n Print a new line character
\t Print a tab character
\r Print a carriage return (used primarily on Windows)
\$ Print the next character as a dollar rather than treating it as part of a variable name
\\ Print the next character as a backslash rather than treating it as an escape character
Here is a code example of these escape sequences in action:
<?php
$MyString = "This is an \"escaped\" string";
$MySingleString = "This \"will\" work";
$MyNonVariable = "I have \$zilch in my pocket";
$MyNewline = "This ends with a line return\n";
$MyFile = "c:\\windows\\system32\\myfile.txt";
?>
Getting an individual byte in a string
<?
$neighbor = "tester";
print $neighbor[3];
?>
Indexing Strings
<?php
$test = "test";
print $test[0];
print $test[2];
?>
PHP 5 Substring Functions
Function Description
substr_count() Counts the number of substring occurrences
strstr() Finds the first occurrence of a string
strchr() Can be used as an alias of strstr()
strrchr() Finds the last occurrence of a character in a string
stristr() Performs the same functionality as strstr() but is case-insensitive
substr_replace() Replaces text within a portion of a string
strops() Finds the position of the first occurrence of a string
substr() Returns a piece of a string
Processing each byte in a string
<?php
$string = "Processing each byte in a string.";
$vowels = 0;
for ($i = 0, $j = strlen($string); $i < $j; $i++) {
if (strstr("aeiouAEIOU",$string[$i])) {
$vowels++;
}
}
?>
Some string examples
<?php
$str = "This is a string";
$str = $str . " with some more text";
$str .= " and a newline at the end.\n";
$num = 9;
$str = "<p>Number: $num</p>";
$num = 9;
$str = "<p>Number: $num</p>";
$str = "This is a test.";
$first = $str[0];
$str = "This is still a test.";
$last = $str[strlen($str)-1];
?>
Supported String Delimiters
CHARACTER REPRESENTATION
\n Newline
\r Carriage return
\t Horizontal tab
\\ Backslash
\$ Dollar sign
\" Double-quotation mark
\[0?7]{1,3} Octal notation regular expression pattern
\x[0?9A?Fa?f]{1,2} Hexadecimal notation regular expression pattern
Using Escaped Characters
<?php
/* The same properly formatted string*/
$variable = "Do you know what \"escaped\" characters are?";
/* Prints the "a" character using hexadecimal */
$variable = "\x41 is the "a" character";
?>
Using {x} notation with strings to read or write individual characters
$mystr = "Jello, world?";
$mystr{0} = "H";
$mystr{12} = "!";
print $mystr;