  PHP
  ,        , ,   ,
 ,       .     
   .    PHP ,    ,   ,
    . ,       (     ,
    (trim-),   )    .

 ,  ,       char,   
. , , ,  ,     ,    
   C/C++,      ,      .
                .

1  htmlspecialchars
htmlspecialchars()



 ,       ,         .        - ,     ,          HTML .  ,   ,             -  (php, javascript  ..)    ,    ! ..   ,      - .  ,       ,           (   )   .   , ,  .

:

string htmlspecialchars(string str [, int quote_style [, string charset]]);

   ,     .          : ENT_QUOTES  ENT_QUOTES.       ,  ,     .     ,   ,    (  ISO-8859-1).

 ,    -  $msg ,     htmlspecialchars()   :

$msg = htmlspecialchars($msg);

 ,  .

  ,           , , ,       .

   php ,    ,   .

<?
  $action = $HTTP_POST_VARS["action"];
  if ( empty($action) )
  {
  ?>
  <HTML>
  <HEAD>
  <TITLE></TITLE>
  </HEAD>
  <BODY>
  <center>
  <table width=1 border=0>
  <form action=test1.php method=post>
  <input type=hidden name=action value=post>
  <tr><td colspan=2><br><textarea cols=50 rows=8 name=msg>
  <? echo $msg; ?>
  </textarea></td></tr>
  <tr><td colspan=2><input type=submit value=''></td></tr>
  </form>
  </table>
  </center>
  </BODY>
  </HTML>
  <?
  }
?>

 ,     .      ,  action  (test1.php). ,      :

<?
  $msg = substr($HTTP_POST_VARS["msg"],0,1024);
  $msg = htmlspecialchars($msg);
  print "<P>".$msg."</P>\n";
?>

   .    $HTTP_POST_VARS  (  substr()   ),     htmlspecialchars()  .

    ,             javascript- (  ):

<Script Language="JavaScript">
  alert("!"); //    JavaScript
</Script>

..     :


 ,        -,         ,  :



<Script Language="JavaScript">alert("!");</Script>

  ,      htmlspecialchars() (  $msg = htmlspecialchars($msg);  test1.php),       ,    :


 , ""      .

2    

substr()
:

string substr(string string, int start[, int length])

    .     ;     ,   ,   (   );      ,   .     ,      .

:

<?
  $string = substr("Hello, world!", 6, 2);
  echo ();
?>

    "w".      , .. 
$string = substr("Hello, world!", 6);


   "world!".



strpos()
:

string strpos(string haystack, string needle[, int offset])

   ,   substr. ..      haystack,       needle. ..  

<?
  $string = strpos("Hello, world!", "world");
  echo($string);
?>

   7.

  offset     ,     :



strrpos()
:

string strrpos(string haystack, string needle)

     haystack  ,    needle.



strstr()
:

string strstr(string haystack, string needle)

 strstr()   ,    haystack,    ,    needle    .      false.

:

<?
  $url = "http://www.softtime.ru";
  $www = strstr($url,"w");
  echo ($www);
?>

: www.softtime.ru

    .  ,   ,  needle   ,           .



strchr()
:

string strchr(string haystack, string needle)

      strstr():

<?
  $url = "http://www.softtime.ru";
  $www = strstr($url,"w");
  echo ($www);
?>

: www.softtime.ru



stristr()
:

string stristr(string haystack, string needle)

      strstr(),     .



strrchr()
:

string strrchr(string haystack, string needle)

       ,      . ..  strrchr()   ,    haystack,    ,    needle    .     false.

  .  ,  needle   ,           .

  ,        .   ,     ( , )      $PATH:

<p>
  <font color=red>  ,     :)</font>
</p>
<?
  echo ($PATH);
?>
<p>
  <font color=red>     :</font>
</p>
<?
  $dir = substr(strrchr($PATH, ";"), 1);
  echo ($dir);
?>

      :



substr_count()
:

int substr_count(string haystack, string needle)

 substr_count()      .     needle,    haystack.

:

<?
  $str = "dfhd@ffs@dfskfk@asas";
  $substr_count = substr_count($str,"@");
  echo ($substr_count);
?>

: 3.



strspn()
:

int strspn(string str1, string str2)

 strspn()      .       str1,    ,     str2.

:

<?
  $str = "dfhd@ffs@dfskfk@asas";
  $substr_count = strspn($str,"df");
  echo ($substr_count);
?>

: 2.



strcspn()
:

int strcspn(string str1, string str2)

  strspn()  strcspn()      .

 strcspn()      str1,     ,     str2.


3  strlen(), chr()  ord()
strlen()
 chr()
 ord()



strlen()
  ,     :

<?
  $string = "Hello, world!!!";
  $string_len = strlen($string);
  echo ($string_len);
?>

chr()
      ASCII         :

<?
  $str = chr(36);
  echo ($str); //   "$"
?>

ord()
 ,   chr():

<?
  $str = ord('$');
  echo($str); //  36
?>

4 Trim- (   )
trim()
 ltrim()
 rtrim()
 chop()



    ,        .      "\n", "\r", "\t", "\v", "\0"   .            -.

trim()
        ,        .

:

<?
  $string = trim("   Hello, world!   ");
?>

  ,  ,      ,          .     :

<?
  $str = "   Hello, world!   ";
  $str1 = trim("   Hello, world!   ");
  $str_len = strlen($str);
  $str1_len = strlen($str1);
  echo("    '$str' = $str_len, <br>
       = $str1_len");
?>

  , ,      :


     'Hello, world!' = 19,
     = 13




ltrim(), rtrim()  chop()
 ltrim()       (..,   ); rtrim()   .  chop()    rtrim().

5   .  
printf()
 sprintf()
 sscanf()



printf()  sprintf()
:

int printf(string format [, mixed args])
string sprintf(string format [, mixed args])

      ,       ,  .  ,             ,   printf()         (  ),  sprintf()       .

          :

<?
  printf("Hello!"); //   "Hello!"
  sprintf("Hello!"); //      ,
  $str = sprintf("Hello!"); //    ,
  printf($str); //       
?>

    ,       ,       .   ,   format     ,   ,    ,    .     ,  ,      ,  .

   "%",          :

 
 ,      .    .         
 
           (..,     ).    ,      
  
   ,     .    ,    ,    
 
           .       ,     ,      
 
      ,     .       :
b   ,    ;
   ,        ASCII ;
d   ,    ;
f     ,     ;
o   ,    ;
s  ;
x   ,       ;
X   ,       
,     ,     printf()     dd/mm/yyyy.       "02/03/2003":

<?
  $day = 2;
  $month = 3;
  $year = 2003;
  printf("%02d/%02d/%04d", $day, $month, $year);
?>

:


  02/03/2003


  ,        ,     .    ,         : %04d

        .       ,   .     .    ,      .     d,        .

   .

<?
  $value = 19;
  printf("%.3f", $value);
?>

    19   19.000



   

 sscanf()
:

mixed sscanf(string str, string format [, string var1 ...])

      printf().    str   format,   printf().          .

   .     ,             "maxtor/203-5505"        .    :

<?
  $product = "maxtor/203-5505";
  $str = sscanf($product,"maxtor/%3d-%4d");
  echo ("$str[0]-$str[1]");
?>

:


  203-5505


 ,        ,     ,         .

       ,      "august 10 2003",         " 10 aug 2003". .

<?
  $date = "august 10 2003";
  list($month, $day, $year) = sscanf($date, "%s %d %d");
  echo("Date: $day-".substr($month,0,3)."-$year");
?>

:


  Date: 10-aug-2003


 :          (    "&"  ).

,      :

<?
  $book = "1\tThinking in PHP";
  $str = sscanf($book,"%d\t%s %s %s", &$id, &$first, &$second, &$last);
  echo ("book number $id - $first $second $last");
?>

6   
convert_cyr_string()
 bin2hex()



convert_cyr_string()
:

string convert_cyr_string(string str, string from, string to)

        .    str   from   to.  from  to   ,  :

k - koi8-r;
w - windows-1251;
i - iso8859-5;
a - x-cp866;
d - x-cp866;
m - x-mac-cyrillic;
     ""   windows-1251  koi8-r  :

<?
  $str1 = "";
  $str2 = convert_cyr_string($str1,"w","k");
  echo ("result of translate '$str1' to koi8-r is '$str2'");
  echo ("<br>");
  $str3 = convert_cyr_string($str2,"k","w");
  echo ("result of translate '$str2' to win is '$str3'");
?>

  :


  result of translate '' to koi8-r is ''
result of translate '' to win is ''




bin2hex()
       .

 bin2hex()            ,    .

       "":
<?
  $str1 = "";
  $str2 = bin2hex($str1);
  echo($str2);
?>

7     
pack()
 unpack()



,     pack()  unpack().       ,        .          ,          .   ,   -   ,     - .

, :

pack()
:

string pack(string format [ ,mixed $args, ...])

 pack()         .         $format,      ,    ,    %.      ,    ,      .   a, A, h  H     ,        ,   -    ( ,      ).     "@",     ,     .           ,      .     *,         . ,        .

  :

a - ,         0;
A - ,    ;
h -  ,    ;
H -  ,    ;
c -   ();
C -  ;
s -   ;
S -   ;
n -   (16 ,    );
v -   (16 ,    );
i -   (     );
I -  ;
l -    (32 ,    );
L -   ;
N -    (32 ,    );
V -   (32 ,    );
f -    ;
d -   ;
x -    ;
X -    1 ;
@ -       .





unpack()
   ,       .   ,   .

:

array unpack(string $format, string $data)

  - .  , .

<?
  $bin = pack("nvn*",0x5722,0x1148, 65, 66); // ,  
  $var = bin2hex($bin); //    
  echo($var);
?>

,   :   6 ,    :

057, 022, 048, 011, 000, 041, 000, 042. ,  .     (nvn*),            ,     ,       (   048, 011,   011, 048),               .

8     

wordwrap()
str_replace()
substr_replace()
 strtr()
stripslashes()
stripcslashes()
 addslashes()
addcslashes()
quotemeta()
 strrev()



wordwrap()
:

string wordwrap(string str [, int width [, string break [, int cut]]])

 wordwrap()         .  ,      str   ,    break, ,        width .      ,    .

 :

<?
  $str = ",  ";
  $mod_str = wordwrap($str,5,"\t");
  echo($mod_str);
?>




str_replace()
:

string str_replace(string from, string to, string str)

 str_replace()     str    . ..     str    from  to   .       .

,  , .  ,    -   , ,  ,             HTML,        ,         ML.  :

  $txt = str_replace("[B]","<B>",$txt);

..          "[B]",       "<B>",   L.

,    ,      ,    ,   .




substr_replace()
:

string substr_replace(string str, string replacement, int start [, int length])

         .    str,        start   length   replacement.    length  ,    .

   start ,       str,    -  .     length,     .    ,   -      str     .




strtr()
:

string strtr(string str, string from, string to)
string strtr(string str, array from)

            .     strtr()   str,    ,    from,      to.   from  to  ,       ,  , .     strtr()  ,     str   ,      from.        ,          .  ,     :

<?
  $str = array("" => "M.Kuznetsov",
  "<name2>" => "I.Simdyanov");
  $str_out = "       !";
  echo strtr($str_out,$str);
?>

           htmlspecialchars():

<?
  $var = array_flip(get_html_translation_table());
  $str = strtr($str, $var);
?>

..,  ,        HTML-,    .




stripslashes()
:

string stripslashes(string str)

   . ..     str       .    : ", ", \.




stripcslashes()
:

string stripcslashes(string str)

        .

  ,      , (   ),     .   C-  (    \n, \r  ..).




addslashes()
:

string addslashes(string str)

      ","  \.        eval().




addcslashes()
:

string addcslashes(string str, string charlist)

      .

  str,           charlist .    ,       -.




quotemeta()
:

string quotemeta(string str)

  .  ,          : . \\ + * ? [ ^ ] ( $ ).         .




strrev()
:

string strrev(string str)

   .

9  / 
str_repeat()
str_pad()
 chunk_split()
strtok()
 explode()
implode()
 join()



str_repeat()
:

string str_repeat(string str, int number)

  .   str   ,     number.

 :

<?
  echo str_repeat("Hello!",3);
  //  Hello! Hello! Hello!
?>



str_pad()
:

string str_pad(string strinput, int pad_length [, string pad_string [, int pad_type]])

        .  strinput   .  pad_length    .     ,   ,    .   pad_string   ,      .    .   pad_type ,      : ,     .  pad_type    :

STR_PAD_RIGHT ( )
STR_PAD_LEFT
STR_PAD_BOTH


chunk_split()
:

string chunk_split(string str [, int chunklen [, string end]])

    .  chunk_split()  ,       str  chunklen (  chunklen = 76)     end ( : end = "\r\n").



strtok()
:

string strtok(string arg1, string arg2)

    .     arg1   arg2.          ,     .       :   arg1   arg2.  ,      arg1   ,      .

:

<?
  $str ="I am very glad to see%you% adhahjasdad";$tok = strtok($str, " ");
  while($tok)
  {
  echo ($tok);
  echo (" ");
  $tok = strtok(" %");
  };
  // : "I" "am" "very" "glad" "to" "see" "you":
?>


  I am very glad to see you



.., ,  ,      ,  .   ,          ,    , ,  ,    ,    .



explode()
:

string explode(string arg, string str [, int maxlimit])

 explode()     .    ,        str,   ,   arg.   maxlimit      .        .

:

<?
  $str = "one two three for five";
  $str_exp = explode(" ", $str);
  /*  $str_exp = array([0] => one, [1] => two,
  [2] => three, [3] => for, [4] => five)
  */
?>



implode()
:

string implode(string var, array param)

 implode()    explode()      .   ,      ,    param,    ,    var.     ,     ""  explode(),     :

:

<?
  $str = "one two three four five";
  $str_exp = explode(" ", $str);
  /* $str_exp = array([0] => one, [1] => two,
  [2] => three, [3] => four, [4] => five)
  */
  $str_imp = implode(" ", $str_exp);
  echo($str_imp);

:


  one two three four five




join()
:

string join(string var, array param)

  implode() -     .

 ,   ,         ,   ,       ,    .       ,        ,         .

10   
strcmp()
strncmp()
 strcasecmp()
strncasecmp()
 strnatcmp()
strnatcasecmp()
 similar_text()
levenshtein()



strcmp()
:

int strcmp(string str1, string str2)

   .      :

0 -    ;
1 - ,  str1   str2;
1  , ,  str1   str2
    , ..       (   ).

:

<?
  $str1 = "ttt";
  $str2 = "tttttttttt";
  echo("Result of strcmp ($str1 , $str2) is ");
  echo(strcmp (str1, str2)); echo("<br>");
  echo("Result of strcmp ($str2, $str1)> is ");
  echo(strcmp (str2, str1)); echo("<br>");
  echo("Result of strcmp ($str1 , $str1) is ");
  echo(strcmp (str1,str1));
?>

:


  Result of strcmp (ttt , tttttttttt) is -1
Result of strcmp (tttttttttt, ttt) is 1
Result of strcmp (ttt, ttt) is 0



strncmp()
:

int strncmp(string str1, string str2, int len)

    strcmp() ,    ,    len .  len     ,    .

      strcmp(), .. :

0 -    ;
1 - ,  str1   str2;
1  , ,  str1   str2
   ,     .



strcasecmp()
:

int strcasecmp(string str1, string str2)

   strcmp(),       .



strncasecmp()
:

int strncasecmp(string str1, string str2, int len)

 strncasecmp() c     .



strnatcmp()
:

int strnatcmp(string str1, string str2)

   ""  .

    .       , ..    ,     . .., ,  ,       pict1.gif, pict20.gif, pict2.gif, pict10.gif,        : pict1.gif, pict10.gif, pict2.gif, pict20.gif.     ,    : pict1.gif, pict2.gif, pict10.gif, pict20.gif.

               .               ,   ,      ,    ,  ,  .

<?
  $array1 = $array2 = array("pict10.gif", "pict2.gif", "pict20.gif", "pict1.gif");
  echo(" :"); echo ("<br>");
  usort ($array1, strcmp);
  print_r ($array1);
  echo ("<br>"); echo(" :"); echo("<br>");
  usort ($array2, strnatcmp);
  print_r ($array2);
?>

   :


   :
Array([0]=>pict1.gif [1]=> pict10.gif [2]=>pict2.gif [3]pict20.gif)
 :
Array([0]=>pict1.gif [1]=> pict2.gif [2]=>pict10.gif [3]pict20.gif)



strnatcasecmp()
:

int strnatcasecmp(string str1, string str2)

 ""     .     ,   strnatcmp(),    .



similar_text()
:

int similar_text(string str_first, string str_second [, double percent])

      .

 similar_text()       .    ,    str_first  str_second.             .

 ,    ,     .      O((max(n,m))3),     . ( ,      N3,  N    .

:

<?
  $str1 = "Hello, world!";
  $str2 = "Hello!";
  $var = similar_text($str1,$str2);
  $var1 = similar_text($str1, $str2, &$tmp);
  //  $tmp   
  echo("   similar_text()
    $str  $str1   :");
  echo("<br>"); echo("$var"); echo("<br>");
  echo("  :"); echo("<br>");
  echo($tmp); //        $tmp
?>

:


     similar_text()   Hello, world!  Hello!   :
6
  :
63.157894736842



levenshtein()
      .

:

int levenshtein(string str1, string str2)
int levenshtein(string str1, string str2, int cost_ins, int cost_rep, int cost_del)
int levenshtein(string str1, string str2, function cost)

  " "    ,    ,     ,    str1  str2.

     O(m*n), ..     str1  str2,      ,   similar_text().

 ,     .              str1  str2:

<?
  $str1 = "Hello, world!";
  $str2 = "Hello!";
  $var = levenshtein($str1,$str2);
  echo($var); //  7
?>

      :    cost_ins,  cost_rep   cost_del. ,       .      ().

<?
  $str1 = "Hello, world!";
  $str2 = "Hello!";
  $var = levenshtein($str1,$str2,3,3,3);
  echo($var); //  21
?>

 21,  ,  7*3 :). ..     ,    (         7)  ,   ,   .   ,   ,   ,   .  ,   ,     . ..,      

$var = levenshtein($str1, str2 ,2,3,6);

     42.

    ,     .

11    URL
parse_url()
parse_str()
 rawurlencode()
rawurldecode()
 base64_encode()
base64_decode()



parse_url()
  URL    .

:

array parse_url(string url)

    ,      URL: "scheme", "host", "port", "user", "pass", "path", "query"  "fragment".

      

:

<?
  $url = "http://www.google.com.ru/search?hl=ru&ie=UTF-8&oe=UTF-8&q=softtime&lr=";
  $arr = parse_url($url);
  print_r($arr);
?>

   :


  Array ( [scheme] => http [host] => www.google.com.ru [path] => /search [query] => hl=ru&ie=UTF-8&oe=UTF-8&q=softtime&lr= )



parse_str()
  URL  .

:

void parse_str(string str [, array arr])

 parse_str()   str ,                 URL. ..      .      arr,  ,     parse_str(),     ,     .    ,   .



rawurlencode()
  URL.

:

string rawurlencode(string str)

 rawurlencode  ,     - ,    "-",   "_"   ".",  :   (%),      ,   .    ,        URL-       .



rawurldecode()
  URL.

:

string rawurldecode(string str)

,      ,  ,     urldecode(),    "+"  .

       -   .

<?
  $url = "http://www.google.com.ru/search?hl=ru&ie=UTF-8&oe=UTF-8&q=&lr=";
  echo(" : $url");echo("<br>");
  echo(":");echo("<br>");
  $url_code = rawurlencode($url);
  echo($url_code);echo("<br>");
  echo(" :");echo("<br>");
  $url_decode = rawurldecode($url_code);
  echo($url_decode);
?>

   :


   : http://www.google.com.ru/search?hl=ru&ie=UTF-8&oe=UTF-8&q=&lr= : http%3A%2F%2Fwww.google.com.ru%2Fsearch%3Fhl%3Dru%26ie%3DUTF-8%26oe%3DUTF-8%26q%3D%F1%EE%F4%F2%F2%E0%E9%EC%26lr%3D  : http://www.google.com.ru/search?hl=ru&ie=UTF-8&oe=UTF-8&q=&lr=



base64_encode()
     MIME base64.

:

string base64_encode(string data)

 base64_encode()  ,    data   MIME base64.

 MIME base64        ,     ,  ,   . ,     Base64    30%  ,  .



base64_decode()
 ,    MIME base64.

:

string base64_decode(string encoded_data)

 base64_decode()  encoded_data    .       , , , .

12   
strtolower()
 strtoupper()
 ucfirst()
 ucwords()



strtolower()
       .

:

string strtolower(string str)

:

<?
  $str = "HELLO WORLD";
  $str = strtolower($str);
  echo $str; //  "hello world"
?>



strtoupper()
     .

:

string strtoupper(string str)

,          .

:

<?
  $str = "Hello World";
  $str = strtoupper($str);
  echo $str;
  //  HELLO WORLD
?>



ucfirst()
       .

:

string ucfirst(string str)

  ,    .       .

<?
  $str = "hello world";
  $str = ucfirst($str);
  echo $str;
  //  Hello world
?>



ucwords()
         .

:

string ucwords(string str)

 ,         .

    ,    : ,    ,  ,  ,    .

<?
  $str = "hello world";
  $str = ucwords($str);
  echo $str;
  //  Hello World
?>


13    3
        ,  ,  , ,      .      ,      - ,          .         ,      -          ,      -  . ,  ,   .

count_chars()
 soundex()
 metaphone()



count_chars()
    .

:

mixed count_chars(string str [, int mode])

 count_chars()      (0-255)   str         mode,     :

0 (  ) -              ;
1 -   0,     str   ;
2 -   0,     ,  ;
3 -  ,     ;
4 -  ,     ;


soundex()
    .

:

string soundex(string str)

 soundex()    ,      ,   ,   ,    ( ),     .

   4 :     3 .



metaphone()
  -.

:

string metaphone(string str)

       soundex(),       ,    str,      ,        .
