PHP/String/String

Материал из Web эксперт
Перейти к: навигация, поиск

An Escape

   <source lang="html4strict">

\ indicates the escape character.

 </source>
   
  


Check string password

   <source lang="html4strict">

<?php $GoodPassword = "asdfg"; $password = "asdf"; if ($password == $GoodPassword){

   print "Password verified!\n"; 

} ?>

 </source>
   
  


Combine an expression with a string

   <source lang="html4strict">

<? $centimeters = 212; print "the width is ".($centimeters/100)." meters"; ?>

 </source>
   
  


Comparing strings

   <source lang="html4strict">

<? $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.";

} ?>

 </source>
   
  


Comparing strings with the equality operator

   <source lang="html4strict">

<? if ("p@demo.ru" == "p@demo.ru") {

  print "Welcome, Mr. President.";

} ?>

 </source>
   
  


curly offset syntax

   <source lang="html4strict">

<?php

  $thing = "php";
  echo $thing;
  echo "
"; echo $thing{0}; echo $thing{1}; echo $thing{2};

?>

 </source>
   
  


Escape Characters in PHP

   <source lang="html4strict">

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)

 </source>
   
  


Escape Characters That Act As Anchors

   <source lang="html4strict">

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)

 </source>
   
  


Escape sequences and their meanings (continued)

   <source lang="html4strict">

\" 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";
   ?>
 
 </source>
   
  


Getting an individual byte in a string

   <source lang="html4strict">

<? $neighbor = "tester"; print $neighbor[3]; ?>

 </source>
   
  


Indexing Strings

   <source lang="html4strict">

<?php

   $test = "test";
   print $test[0]; 
   print $test[2]; 

?>

 </source>
   
  


PHP 5 Substring Functions

   <source lang="html4strict">

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

 </source>
   
  


Processing each byte in a string

   <source lang="html4strict">

<?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++;
   }

} ?>

 </source>
   
  


Some string examples

   <source lang="html4strict">

<?php

   $str = "This is a string";
   $str = $str . " with some more text";
   $str .= " and a newline at the end.\n";
   $num = 9;
$str = "

Number: $num

";
   $num = 9;
$str = "

Number: $num

";
   $str = "This is a test.";
   $first = $str[0];
   
   $str = "This is still a test.";
   $last = $str[strlen($str)-1];

?>

 </source>
   
  


Supported String Delimiters

   <source lang="html4strict">

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

 </source>
   
  


Using Escaped Characters

   <source lang="html4strict">

<?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";

?>

 </source>
   
  


Using {x} notation with strings to read or write individual characters

   <source lang="html4strict">

$mystr = "Jello, world?";

   $mystr{0} = "H";
   $mystr{12} = "!";
   print $mystr;
 
 </source>