$data=range(0,39);//生成连续数据的数组
$res=array_map(function (int $item){
if($item%2===0)return $item;
}, $data);
printf('<pre>%s</pre>',print_r($res,true));
echo '<br>';
$res=array_filter($res,function ($item){
return $item;
});
printf('<pre>%s</pre>',print_r($res,true));
echo '<br>';
$book=['id'=>123,'name'=>'php','price'=>99];
echo <<< BOOK
<ul style="list-style:none">
<li>ID:{$book['id']}</li>
<li>NAME:{$book['name']}</li>
<li>PRICE:{$book['price']}</li>
</ul>
BOOK;
echo '<br>';
class Demo{
const STR=<<< 'FOO'
mail:admin@php.cn<br>
qq:498668472<br>
work-unit:php.cn<br>
FOO;
}
echo Demo::STR;
$handle=fopen('test.txt', 'w');
fprintf($handle,'SELECT * FROM "staff" LIMIT 5');
//更提倡用另一种写法,不要用字面量:
fprintf($handle,sprintf('SELECT * FROM "%s" LIMIT %d','staff',55));
//显示文件:
echo file_get_contents('test.txt');
echo '<br>';
$a=implode(',',[1,2,3,4]);
echo $a.'<br>';
$res=substr_replace(['id:101','id:102','id:103'],['a','b','c'],3,[1,2,3]);
print_r($res);
echo '<br>';
//str_split():字符串变数组;
$res=str_split('php.cn',2);
var_dump($res);
echo '<br>';
//操作csv文件(用,来分隔的文件)
$csvstr=file_get_contents('test2.csv');
$res=str_getcsv($csvstr);
var_dump($res);
echo '<br>';
//str_pad()
echo str_pad('php',10,'=',STR_PAD_RIGHT),'<br>';
echo str_pad('php',10,'=',STR_PAD_LEFT),'<br>';
echo str_pad('php',10,'=',STR_PAD_BOTH),'<br>';
echo '<hr>';
//str_repeat();
echo str_repeat('-*-|', 10);
echo '<br>';
$class='app\home\Index.php';
echo str_replace('\\','/',$class), '<br>';
//注意:如果是linux操作系统,路径分隔符不一样,所以最好这么写:
echo str_replace('\\',DIRECTORY_SEPARATOR,$class), '<br>';
//windows:反斜杠;linux:正斜杠;
echo str_replace('php','*','php.cn,php,thinkphp',$count);
echo $count.'<br>';
$search=['交友','广告','直播','带货'];
$replace=['***','===','###','+++'];
$content='广告代理,直播教学,免费带货,异性交友';
echo str_replace($search, $replace, $content),'<br>';
echo md5('admin888'.'secret...'),'<br>';
echo strip_tags('<h1>防止注入</h1>');
echo '<br>';
//图片编码
$img=base64_encode(file_get_contents('cs1.webp'));
echo $img.'<br>';
echo "<img src='data:image/>jpeg;base64,{$img}'>".'<br>';
//保存到文件中:
file_put_contents('cs2.jpg', base64_decode($img));
echo "<img src='cs2.jpg'>".'<br>';