加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_商丘站长网 (https://www.0370zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php函数怎么快速记忆,每天五个PHP函数记忆

发布时间:2022-10-18 12:48:20 所属栏目:PHP教程 来源:
导读:  每天五个PHP函数记忆

  说明

  本博客是自己的学习笔记,主要参考自 PHP.net

  2019年3月23日 星期六

  is_string — 检测变量是否是字符串

  is_string ( mixed $var ) : b
  每天五个PHP函数记忆
 
  说明
 
  本博客是自己的学习笔记,主要参考自 PHP.net
 
  2019年3月23日 星期六
 
  is_string — 检测变量是否是字符串
 
  is_string ( mixed $var ) : bool
 
  如果 var 是 string 则返回 TRUE,否则返回 FALSE。
 
  preg_replace — 执行一个正则表达式的搜索和替换
 
  preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int KaTeX parse error: Expected 'EOF', got '&' at position 19: …it = -1 [, int &?count ]] ) : mixed
 
  搜索subject中匹配pattern的部分, 以replacement进行替换。
 
  $string = 'April 15, 2003';
 
  $pattern = '/(\w+) (\d+), (\d+)/i';
 
  $replacement = '${1}1,$3';
 
  echo preg_replace($pattern, $replacement, $string);
 
  所以以上程序显示为:
 
  April1,2003
 
  file_get_contents — 将整个文件读入一个字符串
 
  file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] ) : string
 
  和 file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败,file_get_contents() 将返回 FALSE。
 
  file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。
 
  Note:
 
  如果要打开有特殊字符的 URL (比如说有空格)PHP函数,就需要使用 urlencode() 进行 URL 编码。
 
  $homepage = file_get_contents('#39;);
 
  echo $homepage;
 
  array_key_exists — 检查数组里是否有指定的键名或索引
 
  array_key_exists ( mixed $key , array $array ) : bool
 
  数组里有键 key 时,array_key_exists() 返回 TRUE。 key 可以是任何能作为数组索引的值。
 
  $search_array = array('first' => 1, 'second' => 4);
 
  if (array_key_exists('first', $search_array)) {
 
  echo "The 'first' element is in the array";
 
  }
 
  所以以上程序显示为:
 
  The 'first' element is in the array
 
  file_exists — 检查文件或目录是否存在
 
  file_exists ( string $filename ) : bool
 
  $filename = '/path/to/foo.txt';
 
  if (file_exists($filename)) {
 
  echo "The file $filename exists";
 
  } else {
 
  echo "The file $filename does not exist";
 
  }
 
  所以以上程序显示为:
 
  The file /path/to/foo.txt does not exist
 
  2019年3月22日 星期五
 
  sprintf - 返回格式化的字符串
 
  sprintf ( string $format [, mixed $… ] ) : string
 
  返回格式化后的字符串。
 
  $num = 5;
 
  $location = 'tree';
 
  $format = 'There are %d monkeys in the %s';
 
  echo sprintf($format, $num, $location);
 
  所以以上程序显示为:
 
  There are 5 monkeys in the tree
 
  trim — 去除字符串首尾处的空白字符(或者其他字符)
 
  trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] ) : string
 
  此函数返回字符串 str 去除首尾空白字符后的结果。如果不指定第二个参数,trim() 将去除这些字符:
 
  " " (ASCII 32 (0x20)),普通空格符。
 
  “\t” (ASCII 9 (0x09)),制表符。
 
  “\n” (ASCII 10 (0x0A)),换行符。
 
  “\r” (ASCII 13 (0x0D)),回车符。
 
  “\0” (ASCII 0 (0x00)),空字节符。
 
  “\x0B” (ASCII 11 (0x0B)),垂直制表符。
 
  $text = "\t\tThese are a few words :) ... ";
 
  var_dump($text);
 
  print "
 
  ";
 
  $trimmed = trim($text);
 
  var_dump($trimmed);
 
  所以以上程序显示为:
 
  string(32) "These are a few words :) ... "
 
  string(28) "These are a few words :) ..."
 
  strtolower — 将字符串转化为小写
 
  strtolower ( string $string ) : string
 
  将 string 中所有的字母字符转换为小写并返回。
 
  注意 “字母” 与当前所在区域有关。例如,在默认的 “C” 区域,字符 umlaut-A(?)就不会被转换。
 
  $str = "Mary Had A Little Lamb and She LOVED It So";
 
  $str = strtolower($str);
 
  echo $str; // 打印 mary had a little lamb and she loved it so
 
  preg_match — 执行匹配正则表达式
 
  preg_match ( string $pattern , string KaTeX parse error: Expected 'EOF', got '&' at position 18: …bject [, array &?matches [, int $flags = 0 [, int $offset = 0 ]]] ) : int
 
  搜索subject与pattern给定的正则表达式的一个匹配.
 
  //模式分隔符后的"i"标记这是一个大小写不敏感的搜索
 
  if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
 
  echo "A match was found.";
 
  } else {
 
  echo "A match was not found.";
 
  }
 
  所以以上程序显示为:
 
  A match was found.
 
  strpos — 查找字符串首次出现的位置
 
  strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
 
  返回 needle 在 haystack 中首次出现的数字位置。
 
  $mystring = 'abc';
 
  $findme = 'a';
 
  $pos = strpos($mystring, $findme);
 
  // 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作,
 
  // 因为 'a' 是第 0 位置上的(第一个)字符。
 
  if ($pos === false) {
 
  echo "The string '$findme' was not found in the string '$mystring'";
 
  } else {
 
  echo "The string '$findme' was found in the string '$mystring'";
 
  echo " and exists at position $pos";
 
  }
 
  所以以上程序显示为:
 
  The string 'a' was found in the string 'abc' and exists at position 0
 
  2019年3月21日 星期四
 
  in_array — 检查数组中是否存在某个值
 
  in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : bool
 
  大海捞针,在大海(haystack)中搜索针( needle),如果没有设置 strict 则使用宽松的比较。
 
  $os = array("Mac", "NT", "Irix", "Linux");
 
  if (in_array("Irix", $os)) {
 
  echo "Got Irix";
 
  }
 
  //失败,因为 in_array() 是区分大小写的
 
  if (in_array("mac", $os)) {
 
  echo "Got mac";
 
  }
 
  所以以上程序显示为:
 
  Got Irix
 
  explode — 使用一个字符串分割另一个字符串
 
  explode ( string $delimiter , string $string [, int $limit ] ) : array
 
  此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 delimiter 作为边界点分割出来。
 
  // 示例 1
 
  $pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
 
  $pieces = explode(" ", $pizza);
 
  echo $pieces[0]; // piece1
 
  echo $pieces[1]; // piece2
 
  str_replace — 子字符串替换
 
  该函数返回一个字符串或者数组。该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果。
 
  // 赋值:
 
  $bodytag = str_replace("%body%", "black", "
 
  ");
 
  var_dump($bodytag);
 
  implode — 将一个一维数组的值转化为字符串
 
  implode ( string $glue , array $pieces ) : string
 
  implode ( array $pieces ) : string
 
  Note:
 
  因为历史原因,implode() 可以接收两种参数顺序,但是 explode() 不行。不过按文档中的顺序可以避免混淆。
 
  $array = array('lastname', 'email', 'phone');
 
  $comma_separated = implode(",", $array);
 
  echo $comma_separated; // lastname,email,phone
 
  strlen — 获取字符串长度
 
  strlen ( string $string ) : int
 
  $str = 'abcdef';
 
  echo strlen($str); // 6
 
  标签:函数,int,mixed,记忆,file,字符串,array,PHP,string
 

(编辑:开发网_商丘站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!