返回 写小案例无从下...... 登陆

写小案例无从下手,把字符串函数整体练习了一遍

清玉 2019-06-28 10:54:05 223

写小案例无从下手,把字符串函数整体练习了一遍

<?php

//字符串练习

//1、echo
$name = 'harry';
$age = 30;
$gender = 'male';
echo $name ,'<br>';
echo $age ,'<br>';
echo $gender ,'<br>';
echo '姓名:' ,$name ,'  年龄:' ,$age ,'  性别:' ,$gender ,'<br>';
echo '<span style="color:red;">姓名:</span>' ,$name ,'<span style="color:red;">  年龄:</span>' ,$age ,'<span style="color:red;">  性别:</span>' ,$gender ,'<br>';

//2、print
print $name .'<br>';
$a = print $name .$age .$gender .'<br>';
echo $a;
echo '<br>';

//3、print_r()

$arr = [1,2,3,4,5,6];
$obj = new stdClass();
$obj->name = 'marry';
$obj->age = '45';
$obj->gender = 'female';
print_r($arr);
echo '<br>';
print_r($obj);
echo '<br>';

//4、var_dump()
var_dump($arr);
echo '<br>';
var_dump($obj);
echo '<br>';
var_dump($arr,$obj);
echo '<br>';
var_export($arr);
echo '<br>';
echo var_export($arr,true);
echo '<br>';
$arr1 = var_export($arr,true);
echo $arr1;
echo '<br>';

//5、printf()
printf('课程名%s,讲师%s!','php','harry');
printf('成绩%d','100');

echo '<hr>';
//6、trim(),ltrim(),rtrim()
$str = '  admin   ';
echo $str ,'字符长度:' ,strlen($str) ,'<br>';
$str = trim($str);
echo $str ,'过滤后的字符长度:' ,strlen($str) ,'<br>';
$str1 = '123456789';
$str1 = ltrim($str1,'123');
echo '过滤左边字符:' ,$str1 ,'<br>';
$str1 = rtrim($str1,'789');
echo '过滤右边字符:' ,$str1 ,'<br>';

//7、str_pad()
$str2 = '字符串填充';
echo '<span style="background: red;color: #fff;">' ,$str2 ,'</span>' ,' --->数量:' ,strlen($str2) ,'<br>';
$str3 = str_pad($str2,20);
echo '<span style="background: red;color: #fff;">' ,$str2 ,'</span>' ,' --->数量:' ,strlen($str2) ,'<br>';
$str3 = str_pad($str2, 20,'*',STR_PAD_LEFT);
echo '<span style="background: red;color: #fff;">' ,$str3 ,'</span>' ,' --->数量:' ,strlen($str3) ,'<br>';
$str4 = str_pad($str2, 20,'*',STR_PAD_RIGHT);
echo '<span style="background: red;color: #fff;">' ,$str4 ,'</span>' ,' --->数量:' ,strlen($str4) ,'<br>';
$str5 = str_pad($str2, 20,'*',STR_PAD_BOTH);
echo '<span style="background: red;color: #fff;">' ,$str5 ,'</span>' ,' --->数量:' ,strlen($str5) ,'<br>';
echo '<hr>';

//8、strtolower(),strtoupper(),ucfirst(),ucwords()
$str6 = 'MY NAME HARRY';
$str7 = 'my name harry';
$str8 = strtolower($str6);
echo $str8 ,'<br>';
$str9 = strtoupper($str7);
echo $str9 ,'<br>';
$str10 = ucfirst($str7);
echo $str10 ,'<br>';
$str11 = ucwords($str7);
echo $str11 ,'<br>';
echo '<hr>';

//9、nl2br()
$str12 = "我的名字是:\n harry";
$str12 = nl2br($str12);
echo $str12 ,'<br>';

//10、htmlspecialchars()、htmlspecialchars_decode()
$str13 = "<span style='color: red;'>&nbsp;55\"555\"555</span>";
$str13 = htmlspecialchars($str13);
echo $str13 ,'<br>';
$str14 = "&lt;span style='color: red;'&gt;&amp;nbsp;55&quot;555&quot;555&lt;/span&gt;";
$str14 = htmlspecialchars_decode($str14);
echo $str14 ,'<br>';

//11、htmlentities()、htmlentity_decode()
$str15 = "<p>我是一个\"段落\"</p>";
echo $str15;
$str15 = htmlentities($str15);
echo $str15 ,'<br>';
$str16 = '&lt;p&gt;我是一个&quot;段落&quot;&lt;/p&gt;';
$str16 = html_entity_decode($str16);
echo $str16;

//12、strip_tags()
$str17 = "<h2>我是一个<span style='color: blue;'>段落</span></h2>";
echo $str17;
$str17 = strip_tags($str17);
echo $str17 ,'<br>';
$str17 = "<h2>我是一个<span style='color: blue;'>段落</span></h2>";
$str17 = strip_tags($str17,'<span>');
echo $str17 ,'<hr>';

//13、substr()
$str18 = '123456789';
$str19 = substr($str18,2,4);
echo $str19 ,'<br>';

//14、strstr()
$str20 = strstr($str18,'5',true);
echo $str20 ,'<br>';
$str21 = strstr($str18,'5');
echo $str21 ,'<br>';

//15、strpos()
$str22 = '123456123987';
echo strpos($str22,'23') ,'<br>';

//16、str_replace()
$str23 = '这个需要php开发';
$str24 = str_replace('php','java',$str23);
echo $str23 ,'<br>';
echo $str24 ,'<br>';
echo str_replace('php','',$str23) ,'<br>';
echo str_replace(['这个','php'],['那个','java'],$str23) ,'<br>';

//17、str_ireplace()
$str25 = 'abc123ABC';
echo str_ireplace('abc','***',$str25) ,'<br>';

//18、substr_replace()
echo $str23 ,'<br>';
echo strlen($str23) ,'<br>';
echo substr_replace($str23,'java',12,3) ,'<hr>';

//19、urlencode()
$url = urlencode('http://www.baidu.com');
echo $url ,'<br>';

//20、urldecode()
$url = urldecode($url);
echo $url ,'<br>';

//21、http_build_query()
echo http_build_query(['id'=>1,'name'=>3]) ,'<br>';
$url2 = 'http://www.baidu.com/admin/user/index.html?p=2';
echo $url2 ,'<br>';
$url2 = parse_url($url2);
echo '<pre>' .var_export($url2,true) ,'</pre>';
print_r($url2['query']);
echo  '<br>';

//22、json_encode()、json_decode()
$str26 = '我是一个字符串';
$str26 = json_encode($str26);
echo $str26 ,'<br>';
$str26 = json_decode('"\u6211\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32"');
echo $str26 ,'<br>';

$arr2 = ['name'=>'marry','age'=>30];
echo json_encode($arr2) ,'<br>';
$arr2 = json_decode('{"name":"marry","age":30}');
print_r($arr2);
echo '<br>';
$arr2 = json_decode('{"name":"marry","age":30}',true);
print_r($arr2);
echo '<br>';

$obj1 = new stdClass();
$obj1->name = 'harry';
$obj1->age = 24;
echo json_encode($obj1) ,'<br>';
print_r(json_decode('{"name":"harry","age":24}'));
echo '<br>';
print_r(json_decode('{"name":"harry","age":24}',true));


最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网