php 常用函数


strip_tags — 从字符串中去除 HTML 和 PHP 标记

nl2br(); // \n to <br/>

 htmlentities(); //转换所有html标记为相应的html实体  html_entity_decode 反转
 strtoupper(); strtolower(); //转换大小写
 ucfirst(); //只转换第一个字符为大写
 ucwords(); //转换每个words的第一个字母为大写

strrev — 反转字符串

addslashes — 使用反斜线引用字符串

stripslashes — 反引用一个引用字符串

str_shuffle — 随机打乱一个字符串

strip_tags — 从字符串中去除 HTML 和 PHP 标记

substr_count — 计算字串出现的次数

<?php
$text 
'This is a test';
echo 
strlen($text); // 14
echo substr_count($text'is'); // 2

?>

strpos — 查找字符串首次出现的位置

<?php
// 忽视位置偏移量之前的字符进行查找
$newstring 'abcdef abcdef';
$pos strpos($newstring'a'1); // $pos = 7, 不是 0
?>

strrchr — 查找指定字符在字符串中的最后一次出现

<?php
echo strrchr("001.jpg","."); //.jpg
?>

strstr — 查找字符串的首次出现

<?php
$email  
'name@example.com';
$domain strstr($email'@');
echo 
$domain// 打印 @example.com
$user strstr($email'@'true); // 从 PHP 5.3.0 起
echo $user// 打印 name
?>

str_pad — 使用另一个字符串填充字符串为指定长度

<?php
$input "Alien";
echo 
str_pad($input10);                      // 输出 "Alien     "
echo str_pad($input10"-="STR_PAD_LEFT);  // 输出 "-=-=-Alien"
echo str_pad($input10"_"STR_PAD_BOTH);   // 输出 "__Alien___"
echo str_pad($input"___");               // 输出 "Alien_"
?>

mt_rand — 生成更好的随机数

<?php
echo mt_rand() . "\n";
echo 
mt_rand() . "\n";
echo 
mt_rand(515);
?>

floor — 舍去法取整

<?php
echo floor(4.3);   // 4
echo floor(9.999); // 9
?>

ceil — 进一法取整

<?php
echo ceil(4.3);    // 5
echo ceil(9.999);  // 10
?>

round — 对浮点数进行四舍五入

<?php
echo round(3.4);         // 3
echo round(3.5);         // 4
echo round(3.6);         // 4
echo round(3.60);      // 4
echo round(1.955832);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.0452);    // 5.05
echo round(5.0552);    // 5.06
?>
array_multisort() 函数对多个数组或多维数组进行排序。
<?php
//二维数组排序
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 86, 'edition' => 30);
$data[] = array('volume' => 67, 'edition' => 7);
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}
//按$volume升序并按$edition降序
array_multisort($volume, SORT_ASC, $edition, SORT_DESC, $data);

foreach ($data as $key => $value) {
echo "volume:".$value['volume'];
echo "|";
echo "edition:".$value['edition'];
echo "<br/>";
}

// volume:67|edition:7
// volume:67|edition:2
// volume:85|edition:6
// volume:86|edition:30
// volume:86|edition:6
// volume:86|edition:1
// volume:98|edition:2
?>

/**
* curl post body
* @param string $url to request
* @param array $data values to send
* @param array $options for cURL
* @return string
*/
function curlPost($url, $body)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Authorization: OAuth 2.0 token here"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
   if( ! $result = curl_exec($ch))
   {
       trigger_error(curl_error($ch));
   }
   curl_close($ch);
   return $result;
}
       /**
* Send a POST requst using cURL
* @param string $url to request
* @param array $data values to send
* @param array $options for cURL
* @return string
*/
function curlPost($url, array $data = array(), array $options = array())
{
   $defaults = array(
       CURLOPT_POST => 1,
       CURLOPT_HEADER => 0,
       CURLOPT_URL => $url,
       CURLOPT_FRESH_CONNECT => 1,
       CURLOPT_RETURNTRANSFER => 1,
       CURLOPT_FORBID_REUSE => 1,
       CURLOPT_TIMEOUT => 4,
       CURLOPT_POSTFIELDS => http_build_query($data)
   );

   $ch = curl_init();
   curl_setopt_array($ch, ($options + $defaults));
   if( ! $result = curl_exec($ch))
   {
       trigger_error(curl_error($ch));
   }
   curl_close($ch);
   return $result;
}

/**
* Send a GET requst using cURL
* @param string $url to request
* @param array $data values to send
* @param array $options for cURL
* @return string
*/
function curlGet($url, array $data = array(), array $options = array())
{   
   $defaults = array(
       CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($data),
       CURLOPT_HEADER => 0,
       CURLOPT_RETURNTRANSFER => TRUE,
       CURLOPT_TIMEOUT => 4
   );   
   $ch = curl_init();
   curl_setopt_array($ch, ($options + $defaults));
   if( ! $result = curl_exec($ch))
   {
       trigger_error(curl_error($ch));
   }
   curl_close($ch);
   return $result;
//中文转英文
function gb2u($str){
return iconv("GB2312", "UTF-8", $str); 
}
//英文转中文
function u2gb($str){
return iconv("UTF-8", "GB2312", $str); 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值