首页 > foreach

php获取中文字符拼音首字母实例

实例1

function getFirstCharter($str) {
    if (empty($str)) {return '';}
    $fchar = ord($str{0});
    if ($fchar>=ord('A') && $fchar<=ord('z')) return strtoupper($str{0});
    $s1 = iconv('UTF-8', 'gb2312', $str);
    $s2 = iconv('gb2312', 'UTF-8', $s1);
    $s = $s2 == $str ? $s1 : $str;
    $asc = ord($s{0})*256 + ord($s{1}) - 65536;
    if ($asc>=-20319 && $asc<=-20284) return 'A';
    if ($asc>=-20283 && $asc<=-19776) return 'B';
    if ($asc>=-19775 && $asc<=-19219) return 'C';
    if ($asc>=-19218 && $asc<=-18711) return 'D';
    if ($asc>=-18710 && $asc<=-18527) return 'E';
    if ($asc>=-18526 && $asc<=-18240) return 'F';
    if ($asc>=-18239 && $asc<=-17923) return 'G';
    if ($asc>=-17922 && $asc<=-17418) return 'H';
    if ($asc>=-17417 && $asc<=-16475) return 'J';
    if ($asc>=-16474 && $asc<=-16213) return 'K';
    if ($asc>=-16212 && $asc<=-15641) return 'L';
    if ($asc>=-15640 && $asc<=-15166) return 'M';
    if ($asc>=-15165 && $asc<=-14923) return 'N';
    if ($asc>=-14922 && $asc<=-14915) return 'O';
    if ($asc>=-14914 && $asc<=-14631) return 'P';
    if ($asc>=-14630 && $asc<=-14150) return 'Q';
    if ($asc>=-14149 && $asc<=-14091) return 'R';
    if ($asc>=-14090 && $asc<=-13319) return 'S';
    if ($asc>=-13318 && $asc<=-12839) return 'T';
    if ($asc>=-12838 && $asc<=-12557) return 'W';
    if ($asc>=-12556 && $asc<=-11848) return 'X';
    if ($asc>=-11847 && $asc<=-11056) return 'Y';
    if ($asc>=-11055 && $asc<=-10247) return 'Z';
    return null;

阅读全文

php 二维数组按指定的键值排序

具体实例

/*二维数组按指定的键值排序*/
function array_sort($array,$keys,$type='asc'){
 if(!isset($array) || !is_array($array) || empty($array)){
  return '';
 }
 if(!isset($keys) || trim($keys)==''){
  return '';
 }
 if(!isset($type) || $type=='' || !in_array(strtolower($type),array('asc','desc'))){
  return '';
 }
 $keysvalue=array();
 foreach($array as $key=>$val){
  $val[$keys] = str_replace('-','',$val[$keys]);
  $val[$keys] = str_replace(' ','',$val[$keys]);
  $val[$keys] = str_replace(':','',$val[$keys]);
  $keysvalue[] =$val[$keys];
 }
 asort($keysvalue); //key值排序
 reset($keysvalue); //指针重新指向数组第一个
 foreach($keysvalue as $key=>$vals) {
  $keysort[] = $key;
 }
 $keysvalue = array();
 $count=count($keysort);
 if(strtolower($type) != 'asc'){
  for($i=$count-1; $i>=0; $i--) {
   $keysvalue[] = $array[$keysort[$i]];
  }
 }else{
  for($i=0; $i<$count; $i++){
   $keysvalue[] = $array[$keysort[$i]];
  }
 }
 return $keysvalue;
}

阅读全文

PHP采集程序常用的采集函数收藏

这几天关注了一下PHP的采集程序,才发现用PHP采集内容是这么方便,把经常用到的采集函数在这里总结一下,方便以后使用!

获取所有链接内容和地址
function getAllURL(http://pic2.phprm.com/2013/08/13/$code.jpg){
preg_match_all('/<as+href=["|']?([^>"' ]+)["|']?s*[^>]*>([^>]+)</a>/i',$code,$arr);
return array('name'=>$arr[2],'url'=>$arr[1]);
}

阅读全文

php 获取客户端的ip地址程序代码

最简单的做法


function getRealIpAddr()
{
  if (!emptyempty($_SERVER['HTTP_CLIENT_IP']))
  {
    $ip=$_SERVER['HTTP_CLIENT_IP'];
  }
  elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR']))
  //to check ip is pass from proxy
  {
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  }
  else
  {
    $ip=$_SERVER['REMOTE_ADDR'];
  }
  return $ip;
}

阅读全文

PHP封装数据库操作类(链接数据库)

有面向对象技术基础的编程人员看一天就可以写起来了。而PHP在访问数据库的时候又经常会出现各种问题,如字符编码问题、SQL语法错误问题、PHP处理数据记录对象和返回对象的问题等。我这里写了一个数据库操作类,封装了数据库增删添改等操作,很方便使用。用这个类,可以加速网站的后台开发。

阅读全文

Magento 获取购物车产品数量和价格程序代码

得到购物车所有的产品信息

// $items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();foreach($items as $item) {
    echo 'ID: '.$item->getProductId().'<br>';
    echo 'Name: '.$item->getName().'<br>';
    echo 'Sku: '.$item->getSku().'<br>';
    echo 'Quantity: '.$item->getQty().'<br>';
    echo 'Price: '.$item->getPrice().'<br>';
    echo "<br>";
}

阅读全文

PHP移除指定HTML标签方法总结

有时候我们需要把html标签页存到数据库里,但是有些场合却需要拿无html标签的纯数据,这个时候就要对带html标签的数据进行处理,把html标签都去掉。平时用 htmlspecialchars() 来过滤html,但是把html的字符转义了,最后显示出来的就是html源代码,利用strip_tags()就可以把html标签去除掉。

阅读全文