字符串操作笔记记录
分割字符串
foreach($arr as $i) php的循环遍历
foreach($arr as $key=>$value) php的循环遍历
for ($j=0; $j < count($arr); $j++) 常规的循环遍历
<?php
$str = "100,200,300,400,500";
$arr = explode(",", $str);
foreach($arr as $i){
echo $i.'<br>';
}
?>
//另外一个循环
for ($j=0; $j < count($arr); $j++)
{
echo "我的号码是:$j: $arr[$j]<br>";
}
foreach ($arr as $key => $value)
{
echo "数字序列号:".$key."<br>";
}
字符串替换
str_replace(find,replace,str); 字符串的替换函数
find 替换的元素
replace 需要替换的元素
str 原数据
$title = "6000.jpg";
$result = str_replace(".jpg",'.png', $title);
echo "替换结果".$result;
裁剪字符串
使用substr(str,pos) 获取字符串
strrpos(str,element) 获取对应的目标位置
in_array(str,array) 检索是否存在
$name = "5000.jpeg";
$type = strtolower(substr($name,strrpos($name, ".")+1));
//判断类型
if(in_array($type, array("jpg","gif","jpeg","png")))
{
echo "是图片后缀";
}
输出一个alert
function alert($text)
{
echo "<script type='text/javascript'>alert(\"".$text."\");</script>";
}
简单地使用curl 函数的,post 和get数据请求。
function httpGet($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function httpPost($url,$data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}