PHP中函数的意义和用法

1. mb_strlen

    版本:(PHP 4 >= 4.0.6, PHP 5, PHP 7)

    解释:mb_strlen — 获取字符串的长度 

   使用方式mixed mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )

   参数: str:要检查长度的字符串。 encoding 参数:为字符编码。如果省略,则使用内部字符编码。

   返回值:返回具有 encoding 编码的字符串 str 包含的字符数。 多字节的字符被计为 1。如果给定的 encoding 无效则返回 FALSE。

                    mb_internal_encoding() - 设置/获取内部字符编码

                    grapheme_strlen() - Get string length in grapheme units

                    iconv_strlen() - 返回字符串的字符数统计

                    strlen() - 获取字符串长度

捕获13.GIF

2. mb_internal_encoding

     版本:(PHP 4 >= 4.0.6, PHP 5, PHP 7)

      解释:mb_internal_encoding — 设置/获取内部字符编码

     使用方式mixed mb_internal_encoding ([ string $encoding = mb_internal_encoding() ] )

     参数: encoding 字符编码名称使用于 HTTP 输入字符编码转换、HTTP 输出字符编码转换、mbstring 模块系列函数字符编码转换的默认编码。 You should notice that the internal encoding is totally different from the one for multibyte regex.

     返回值如果设置了 encoding,则成功时返回 TRUE, 或者在失败时返回 FALSE。 In this case, the character encoding for multibyte regex is NOT changed. 如果省略了 encoding,则返回当前的字符编码名称。

         例子:      <?php
                /* 设置内部字符编码为 UTF-8 */
                                mb_internal_encoding("UTF-8");
                  /* 显示当前的内部字符编码*/

              echo mb_internal_encoding();
                                ?>

3. strlen

       版本:(PHP 4, PHP 5, PHP 7)

      解释:strlen — 获取字符串长度

     使用方式int strlen ( string $string )

     参数: string:需要计算长度的字符串

     返回值成功则返回字符串 string 的长度;如果 string 为空,则返回 0。

        例子:          <?php
                                   $str = 'abcdef';
                                   echo strlen($str); // 6
                                   $str = ' ab cd ';
                                   echo strlen($str); // 7
                                  ?>

4. addslashes

      版本:(PHP 4, PHP 5, PHP 7)

      解释:addslashes — 使用反斜线引用字符串使用 addslashes() 的例子是当你要往数据库中输入数据时,需要对其进行转义。如果你使用的 DBMS 没有一个转义函数,并且使用 \ 来转义特殊字符,你可以使用这个函数。 仅仅是为了获取插入数据库的数据,额外的 \ 并不会插入。

     使用方式string addslashes ( string $str )

     参数: str:要转义的字符。

     返回值返回转义后的字符。

        例子:       <?php
                                 $str 
"Is your name O'reilly?";
 // 输出: Is your name O\'reilly?
               echo addslashes($str);
                             ?>

      捕获14.GIF

5.var_dump

     版本:(PHP 4, PHP 5, PHP 7)

      解释:var_dump — 打印变量的相关信息

     使用方式void var_dump ( mixed $expression [, mixed $... ] )

     参数: expression你要打印的变量。

     返回值没有返回值。

        例子:      捕获16.GIF


6.bin2hex

    版本:(PHP 4, PHP 5, PHP 7)

      解释:bin2hex — 函数把包含数据的二进制字符串转换为十六进制值

     使用方式string bin2hex ( string $str )

     参数: str:要转义的字符。

     返回值返回指定字符串十六进制的表示

        例子:      捕获16.GIF

7.chop,rtrim

     版本:(PHP 4, PHP 5, PHP 7)

      解释:chop — rtrim() 的别名,rtrim — 删除字符串末端的空白字符(或者其他字符)

     使用方式string rtrim ( string $str [, string $character_mask ] )该函数删除 str 末端的空白字符(或者其他字符)并返回。

     参数: str:要转义的字符。character_mask:通过指定 character_mask,可以指定想要删除的字符列表。简单地列出你想要删除的全部字符。使用 .. 格式,可以指定一个范围。

     返回值返回改变后的字符串。

       例子:      <?php

               $text "\t\tThese are a few words :) ...  ";
             $binary "\x09Example string\x0A";
             $hello  "Hello World";
               var_dump($text$binary$hello);
               print "\n";
               $trimmed rtrim($text);
               var_dump($trimmed);

               $trimmed rtrim($text" \t.");
               var_dump($trimmed);

               $trimmed rtrim($hello"Hdle");
               var_dump($trimmed);
               // 删除 $binary 末端的 ASCII 码控制字符
               // (包括 0 - 31)
               
$clean rtrim($binary"\x00..\x1F");
               var_dump($clean);            ?>

以上例子会输出:

                    string(32) "        These are a few words :) ...  "
                   string(16) "    Example string
                   "
                   string(11) "Hello World"
                   string(30) "        These are a few words :) ..."
                   string(26) "        These are a few words :)"
                   string(9) "Hello Wor"
                   string(15) "    Example string"

捕获18.GIF

8.chr

    版本:(PHP 4, PHP 5, PHP 7)

      解释:chr — 返回指定的字符

     使用方式sstring chr ( int $ascii )

     参数: ascii Ascii 码。

     返回值返回规定的字符。

       例子:      <?php
             $str 
"The string ends in escape: ";
             $str .= chr(27); /* 在 $str 后边增加换码符 */
                /* 通常这样更有用 */
             $str sprintf("The string ends in escape: %c"27);
            ?>

            捕获20.GIF


9.ord

    版本:(PHP 4, PHP 5, PHP 7)

      解释:ord — 返回字符的 ASCII 码值

     使用方式int ord ( string $string )

     参数string一个字符。

     返回值返回整型的 ASCII 码值

       例子:    <?php
            $str 
"\n";
            if (
ord($str) == 10) {
            echo 
"The first character of \$str is a line feed.\n";
            }
           ?>

捕获23.GIF    

10.crypt

    版本:(PHP 4, PHP 5, PHP 7)

      解释:crypt — 单向字符串散列

     使用方式string crypt ( string $str [, string $salt ] ))

     参数str待散列的字符串。salt可选的盐值字符串。如果没有提供,算法行为将由不同的算法实现决定,并可能导致不可预料的结束。

     返回值返回散列后的字符串或一个少于 13 字符的字符串,从而保证在失败时与盐值区分开来。

       例子:    <?php
                // 设置密码
             $password 'mypassword';
               // 获取散列值,使用自动盐值
             $hash crypt($password);
          ?>

11.htmlentities

    版本:(PHP 4, PHP 5, PHP 7)

      解释:htmlentities — 将字符转换为 HTML 转义字符

     使用方式string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )

                  如果要解码(反向操作),可以使用 html_entity_decode()

     参数string输入字符。flags以下一组位掩码标记,用于设置如何处理引号、无效代码序列、使用文档的类型。 默认是 ENT_COMPAT | ENT_HTML401double_encode关闭 double_encode 时,PHP 不会转换现有的 HTML 实体, 默认是全部转换。

         捕获4.GIF

     返回值返回编码后的字符。如果指定的编码 encoding 里, string 包含了无效的代码单元序列, 没有设置 ENT_IGNORE 或者 ENT_SUBSTITUTE 标记的情况下,会返回空字符串。

       例子:   <?php
            $str 
"A 'quote' is <b>bold</b>";

               // 输出: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
            echo htmlentities($str);
              // 输出: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
            echo htmlentities($strENT_QUOTES);
           ?>

           捕获28.GIF

捕获29.GIF

捕获30.GIF

12.implode

    版本:(PHP 4, PHP 5, PHP 7)

      解释:implode — 将一个一维数组的值转化为字符串

     使用方式string implode ( string $glue , array $pieces )

             string implode ( array $pieces )  用 glue 将一维数组的值连接为一个字符串。

     参数sglue默认为空的字符串。pieces你想要转换的数组。

     返回值返回一个字符串,其内容为由 glue 分割开的数组的值。

       例子:   <?php
            $array 
= array('lastname''email''phone');
            $comma_separated implode(","$array);
            echo 
$comma_separated// lastname,email,phone
                  // Empty string when using an empty array:
            var_dump(implode('hello', array())); // string(0) ""
           ?>

                        捕获32.GIF

13.md5

    版本:(PHP 4, PHP 5, PHP 7)

      解释:md5 — 计算字符串的 MD5 散列值

     使用方式string md5 ( string $str [, bool $raw_output = false ] )

     参数str原始字符串。raw_output如果可选的 raw_output 被设置为 TRUE,那么 MD5 报文摘要将以16字节长度的原始二进制格式返回。

     返回值以 32 字符十六进制数字形式返回散列值。

       例子:   <?php
             $str 
'apple';

             if (md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
             echo 
"Would you like a green or red apple?";
              }
           ?>

            捕获33.GIF 捕获34.GIF

捕获35.GIF

14.str_replace()

    版本:(PHP 4, PHP 5, PHP 7)

      解释:str_replace — 子字符串替换

     使用方式mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) 该函数返回一个字符串或者数组。该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果。

     参数如果 search 和 replace 为数组,那么 str_replace() 将对 subject 做二者的映射替换。如果 replace 的值的个数少于 search 的个数,多余的替换将使用空字符串来进行。如果 search 是一个数组而 replace 是一个字符串,那么 search 中每个元素的替换将始终使用这个字符串。该转换不会改变大小写。 

              如果 search 和 replace 都是数组,它们的值将会被依次处理。

               search查找的目标值,也就是 needle。一个数组可以指定多个目标。replace是search 的替换值。一个数组可以被用来指定多重替换。subject执行替换的数组或者字符串。也就是 haystack。如果 subject 是一个数组,替换操作将遍历整个 subject,返回值也将是一个数组。count如果被指定,它的值将被设置为替换发生的次数。

     返回值该函数返回替换后的数组或者字符串。

       例子:  

 <?php
// 赋值: <body text='black'>
$bodytag str_replace("%body%""black""<body text='%body%'>");
// 赋值: Hll Wrld f PHP
$vowels = array("a""e""i""o""u""A""E""I""O""U");
$onlyconsonants str_replace($vowels"""Hello World of PHP");
// 赋值: You should eat pizza, beer, and ice cream every day
$phrase  "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits""vegetables""fiber");
$yummy   = array("pizza""beer""ice cream");
$newphrase str_replace($healthy$yummy$phrase);
// 赋值: 2
$str str_replace("ll""""good golly miss molly!"$count);
echo 
$count;
?>

 

15.str_split()

    版本:(PHP 5, PHP 7)

      解释:str_split — 将字符串转换为数组

     使用方式array str_split ( string $string [, int $split_length = 1 ] ) 将一个字符串转换为数组。

     参数string输入字符串。split_length每一段的长度

     返回值如果指定了可选的 split_length 参数,返回数组中的每个元素均为一个长度为 split_length 的字符块,否则每个字符块为单个字符。

                        如果 split_length 小于 1,返回 FALSE。如果 split_length 参数超过了 string 超过了字符串 string 的长度,整个字符串将作为数组仅有的一个元素返回        

    例子:

捕获5.GIF    

捕获6.GIF

捕获39.GIF

16.strcasecmp

    版本:(PHP 4, PHP 5, PHP 7)

      解释:strcasecmp — 二进制安全比较字符串(不区分大小写)

     使用方式int strcasecmp ( string $str1 , string $str2 )二进制安全比较字符串(不区分大小写)。

     参数str1第一个字符串。str2第二个字符串

     返回值如果 str1 小于 str2 返回 < 0; 如果 str1 大于 str2 返回 > 0;如果两者相等,返回 0。

       例子:

     捕获7.GIF


17.strstr

    版本:(PHP 4, PHP 5, PHP 7)

      解释:strstr — 查找字符串的首次出现

     使用方式string strstr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) 返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。 该函数区分大小写。如果想要不区分大小写,请使用 stristr()如果你仅仅想确定 needle 是否存在于 haystack 中,请使用速度更快、耗费内存更少的 strpos() 函数。

     参数haystack输入字符串。needle如果 needle 不是一个字符串,那么它将被转化为整型并且作为字符的序号来使用。before_needle若为 TRUEstrstr() 将返回 needle 在 haystack 中的位置之前的部分。

     返回值返回字符串的一部分或者 FALSE(如果未发现 needle)。

       例子:

      捕获8.GIF

捕获41.GIF

18.substr

    版本:(PHP 4, PHP 5, PHP 7)

      解释:substr — 返回字符串的子串

     使用方式string substr ( string $string , int $start [, int $length ] )返回字符串 string 由 start 和 length 参数指定的子字符串。

     参数string输入字符串。必须至少有一个字符。start如果 start 是非负数,返回的字符串将从 string 的 start 位置开始,从 0 开始计算。例如,在字符串 “abcdef” 中,在位置 0 的字符是 “a”,位置 2 的字符串是 “c” 等等。

                 如果 start 是负数,返回的字符串将从 string 结尾处向前数第 start 个字符开始。如果 string 的长度小于 start,将返回 FALSE

       返回值返回提取的子字符串, 或者在失败时返回 FALSE

     例子:

        捕获9.GIF        捕获42.GIF

19.preg_match

    版本:(PHP 4, PHP 5, PHP 7)

      解释:preg_match — 执行匹配正则表达式

     使用方式int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )搜索subjectpattern给定的正则表达式的一个匹配.

     参数pattern要搜索的模式,字符串类型。subject输入字符串。matches如果提供了参数matches,它将被填充为搜索结果。 $matches[0]将包含完整模式匹配到的文本, $matches[1] 将包含第一个捕获子组匹配到的文本,以此类推。

     返回值preg_match()返回 pattern 的匹配次数。 它的值将是0次(不匹配)或1次,因为preg_match()在第一次匹配后 将会停止搜索。preg_match_all()不同于此,它会一直搜索subject 直到到达结尾。 如果发生错误preg_match()返回 FALSE

       例子:

      捕获10.GIF     捕获11.GIF

捕获44.GIF

20.settype

    版本:(PHP 4, PHP 5, PHP 7)

      解释:settype — 设置变量的类型

     使用方式bool settype ( mixed &$var , string $type )将变量 var 的类型设置成 type

     参数var要转换的变量。type 的可能值为:boolean” (或为“bool”,从 PHP 4.2.0 起);“integer” (或为“int”,从 PHP 4.2.0 起);“float” (只在 PHP 4.2.0 之后可以使用,对于旧版本中使用的“double”现已停用);"string";"array";"object";“null” (从 PHP 4.2.0 起);

     返回值成功时返回 TRUE, 或者在失败时返回 FALSE

       例子:

       捕获12.GIF


  




猜你喜欢

转载自blog.51cto.com/13905896/2166192