php第五节(字符串函数)

<?php

//查找字符串函数
// strpos() 查找字符第一次出现的位置 重点区分大小写
//stripos — 查找字符串首次出现的位置(不区分大小写)
//strrpos — 计算指定字符串在目标字符串中最后一次出现的位置

 $str = "hello world";
 $position = strpos($str, 'e');
// $position = stripos($str, 'E');
// $position = strrpos($str, 'l');
 echo $position;

//提取子字符串
//substr()  返回字符串的子串  指定位置之间的字符串 常用
   $str="hello world";
  $res= substr($str, 1,6);
  echo $res;
//strstr — 查找字符串的首次出现
$newStr = strstr($str, 'e', true);
//stristr — strstr() 函数的忽略大小写版本
$newStr = stristr($str, 'E', true);
//strrchr — 查找指定字符在字符串中的最后一次出现
$newStr = strrchr($str, 'l');

 //替换字符串的PHP字符串函数
 // str_replace — 子字符串替换

$str = "hello world";
$count = null;//显示替换几次
//第一个要替换的字符  第二个替换成的字符 第三个参数替换的字符串
$newStr = str_replace('o', 'w', $str, $count)
$newStr = str_replace(['o','l'], ['w','p'], $str, $count)
$echo $newStr;

//字符长度:
//strlen — 获取字符串长度
$str = "hello world";
echo strlen($str);

//分割字符串
// explode — 使用一个字符串分割另一个字符串
//str_split — 将字符串转换为数组

$str = "hello/world";
$res=explode("/",$str);
$rest=str_split($str,5);
var_dump($res);
echo "<br/>";
echo $res[0];
echo "<br/>";
 print_r($rest);

//去除空格:
// trim — 去除字符串首尾处的空白字符(或者其他字符)
$str = 'hello world echo <h1>welcome to sixstaredu<h1>';
$str = trim($str);
$str = ltrim($str);
$str = rtrim($str);
echo $str;
echo $newStr;

//html标记相关的函数
//strip_tags()  去除html标记和php标记

$str = "<div>hello <h1>world</h1></div>";
$newStr = strip_tags($str, "<div>");//想保留的标签写在第二个参数
echo $newStr;

//htmlspecialchars — 将特殊字符转换为 HTML 实体

$str = "<div>hello <h1>world</h1></div>"; // 
$newStr = htmlspecialchars($str);//将标签作为文本输出
echo $newStr;

//字符大小写转换
//strtolower() 将字符串转成小写

$str="HELLO WORLD";
$newStr=strtolower($str);
echo $newStr;

strtoupper($str) 字符串转换为大写

$str="hello world";
$newStr=strtoupper($str);
echo $newStr;

ucfirst($str) 将字符串的第一个字符转换为大写

$str= "hello world";
$newStr=ucfirst($str);
echo $newStr;

ucwords($str) 将每个单词的首字母转换为大写

$str = "hello world";
echo ucwords($str);

//数学函数
// abs() 绝对值 absolute
$a=-2;
echo abs($a)

//ceil() 向上取整
//floor() 向下取整
//round — 对浮点数进行四舍五入
$var = 3.5615;
var_dump(ceil($var));//4
var_dump(floor($var));//3
var_dump(round($var)); //4
//mt_rand() 产生随机数
$str =mt_rand();
$str = mt_rand(1000,9999);
echo mt_rand(0, 10) / 10;
echo $str;

//max — 找出最大值
//min — 找出最小值
$a=5;
$b= 8;
$c = 50;
echo max($a,$b,$c);
echo "<br/>";
echo min($a, $b, $c);
echo "<br/>";
$arr=[55,99,552];
echo max($arr);
echo "<br/>";
echo min($arr);

/**
 * 时间日期函数
 *  date_default_timezone_set('Asia/Shanghai');设置时区
 * date_default_timezone_get() 获取时区
 * ini_set('date.timezone','PRC');  date.timezone;设置时区
 */
echo date("Y-m-d H:i:s", time());
date_default_timezone_set('Asia/Shanghai');
echo date_default_timezone_get();

//date() 格式化时间
echo date("Y-m-d H:i:s", );

//getdate() 获取时间和日期
print_r(getdate());

 //获取当前时间戳
  echo time();

//microtime() 获取微秒
$f = microtime(true);
var_dump($f);

//strtotime  :将任何字符串的日期时间描述解析为 Unix 时间戳

 var_dump(strtotime("2018-9-13 22:37:35"));
$res=strtotime("2018-12-10 13:37:35");
echo "<pre>";
 print_r(getdate($res));

?>

猜你喜欢

转载自www.cnblogs.com/lmh951126/p/10148264.html
今日推荐