PHP正则替换HTML代码中的所有图片链接地址
function handleDescriptionHtml($description,$http_path)
{
$description = htmlspecialchars_decode($description);
$pregRule = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
$description = preg_replace($pregRule, '<img src="'.$http_path. '${1}" />', $description);
return $description;
}
获取域名
function protocol()
{
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
return $protocol.$_SERVER[HTTP_HOST];
}
转换字符编码为utf-8
function strToUtf8($str){
$encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
if($encode == 'UTF-8'){
return $str;
}else{
return mb_convert_encoding($str, 'UTF-8', $encode);
}
}
二维数组排序
function arraySort($array,$keys,$sort='asc') {
$newArr = $valArr = array();
foreach ($array as $key=>$value) {
$valArr[$key] = $value[$keys];
}
($sort == 'asc') ? asort($valArr) : arsort($valArr);
reset($valArr);
foreach($valArr as $key=>$value) {
$newArr[$key] = $array[$key];
}
return $newArr;
}
curl_get
function curl_get($url){
$header = array(
'Accept: application/json',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($curl);
if (curl_error($curl)) {
return false;
} else {
curl_close($curl);
return $data;
}
}
curl_post
function curl_post( $url, $postdata ) {
$header = array(
'Accept: application/json',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE );
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$data = curl_exec($curl);
if (curl_error($curl)) {
return false;
} else {
curl_close($curl);
return $data;
}
}
获取两个时间相差几天
function getTimeDifferDay($time1,$time2)
{
return floor((strtotime($time1) - strtotime($time2))/86400);
}
获取两个时间相差几小时
function getTimeDifferHour($time1,$time2)
{
$day = getTimeDifferDay($time1,$time2);
$data = ceil((strtotime($time1) - strtotime($time2)) % 86400 / 3600);
if ($day > 0) {
$data += $day*24;
}
return $data;
}
获取两个时间相差几分钟
function getTimeDifferMinute($time1,$time2)
{
$day = getTimeDifferDay($time1,$time2);
$data = ceil((strtotime($time1) - strtotime($time2)) % 86400 / 60);
if ($day > 0) {
$data += $day*24*60;
}
return $data;
}
获取两个时间相差几秒
function getTimeDifferSecond($time1,$time2)
{
return strtotime($time1) - strtotime($time2);
}
获取随机字符串
function getRand( $length = 6,$isAll = false)
{
if ($isAll) {
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
} else {
$chars = '0123456789';
}
$randStr = '';
for ( $i = 0; $i < $length; $i++ )
{
$randStr .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $randStr;
}
|