首页 > 编程技术 > php

php 中英文混合字符串截取

发布时间:2016-11-25 17:31

文章介绍一个实用的函数,我们如果用php substr来截取字符在中文上处理的很有问题,今天自己写了一个比较好的中文与英文字符截取的函数,有需要的朋友可以参考下。
 代码如下 复制代码

function smssubstr($string, $length) {
 if(strlen($string) <= $length) {
  return $string; 
 }
 $strcut = '';
 for($i = 0; $i < $length; $i++) {
  $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
 }
 return $strcut;
}

for($i=1; $i<=$smsnum; $i++){
 ${'smnrent'.$i} = smssubstr($message,$smsper);
 $message = str_replace(${'smnrent'.$i},"",$message);
}

好了,有需要的朋友拿去吧,原理我也不说多了,可以用就行了。

很多时候需要用到js的escape函数来转换中文字符,可是用js转换后的字符怎么用php来转换回来呢,下面我就找到了两个很实用的函数。

GB2312编码:

 代码如下 复制代码

function unescape($str) {
$str = rawurldecode($str);
preg_match_all("/%u.{4}|&#x.{4};|&#d+;|.+/U",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(substr($v,0,2) == "%u")
$ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,-4)));
elseif(substr($v,0,3) == "&#x")
$ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,3,-1)));
elseif(substr($v,0,2) == "&#") {
$ar[$k] = iconv("UCS-2","GBK",pack("n",substr($v,2,-1)));
}
}
return join("",$ar);
}


UTF8编码:

 代码如下 复制代码

function unescape($str){
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++){
if ($str[$i] == '%' && $str[$i+1] == 'u'){
$val = hexdec(substr($str, $i+2, 4));
if ($val < 0x7f) $ret .= chr($val);
else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));
$i += 5;
}
else if ($str[$i] == '%'){
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
}
else $ret .= $str[$i];
}
return $ret;
}

以前也经常介绍关于无限分类这个问题,大多数情况我们都是利用php递归来实现,今天介绍的这篇无限分类不需要递归哦,方法很简单有需要的朋友可以参考一下。

 

 代码如下 复制代码

//////////////
//////无限分类的数据库设计及样例
//////////////
mysql> create database db_kind;
Query OK, 1 row affected

mysql> use db_kind;
Database changed
mysql> create table tb_kind(
-> id int not null auto_increment primary key,
-> pid int,
-> path varchar(200)
-> );
Query OK, 0 rows affected

mysql> insert into tb_kind values(null,"新闻",0,0);
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"视频",0,0);
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"图片",0,0);
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"博客",0,0);
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"体育新闻",1,"0-1");
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"娱乐新闻",1,"0-1");
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"财经新闻",1,"0-1");
Query OK, 1 row affected

mysql> select * from db_kind;
ERROR 1146 : Table 'db_kind.db_kind' doesnot exist
mysql> select * from tb
_kind;
+----+----------+-----+------+
| id | pname | pid | path |
+----+----------+-----+------+
| 1 | 新闻 | 0 | 0 |
| 2 | 视频 | 0 | 0 |
| 3 | 图片 | 0 | 0 |
| 4 | 博客 | 0 | 0 |
| 5 | 体育新闻 | 1 | 0-1 |
| 6 | 娱乐新闻 | 1 | 0-1 |
| 7 | 财经新闻 | 1 | 0-1 |
+----+----------+-----+------+
7 rows in set
mysql> insert into tb_kind values(null,"篮球新闻",5,"0-1-5");
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"足球新闻",5,"0-1-5");
Query OK, 1 row affected

mysql> select * from tb_kind;
+----+----------+-----+-------+
| id | pname | pid | path |
+----+----------+-----+-------+
| 1 | 新闻 | 0 | 0 |
| 2 | 视频 | 0 | 0 |
| 3 | 图片 | 0 | 0 |
| 4 | 博客 | 0 | 0 |
| 5 | 体育新闻 | 1 | 0-1 |
| 6 | 娱乐新闻 | 1 | 0-1 |
| 7 | 财经新闻 | 1 | 0-1 |
| 8 | 篮球新闻 | 5 | 0-1-5 |
| 9 | 足球新闻 | 5 | 0-1-5 |
+----+----------+-----+-------+
9 rows in set

mysql> insert into tb_kind values(null,"NBA",8,"0-1-5-8");
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"CBA",8,"0-1-5-8");
Query OK, 1 row affected

mysql> select * from tb_kind;
+----+----------+-----+---------+
| id | pname | pid | path |
+----+----------+-----+---------+
| 1 | 新闻 | 0 | 0 |
| 2 | 视频 | 0 | 0 |
| 3 | 图片 | 0 | 0 |
| 4 | 博客 | 0 | 0 |
| 5 | 体育新闻 | 1 | 0-1 |
| 6 | 娱乐新闻 | 1 | 0-1 |
| 7 | 财经新闻 | 1 | 0-1 |
| 8 | 篮球新闻 | 5 | 0-1-5 |
| 9 | 足球新闻 | 5 | 0-1-5 |
| 10 | NBA | 8 | 0-1-5-8 |
| 11 | CBA | 8 | 0-1-5-8 |
+----+----------+-----+---------+
11 rows in set

mysql> select concat(path,"-",id) from tb_kind;
+---------------------+
| concat(path,"-",id) |
+---------------------+
| 0-1 |
| 0-2 |
| 0-3 |
| 0-4 |
| 0-1-5 |
| 0-1-6 |
| 0-1-7 |
| 0-1-5-8 |
| 0-1-5-9 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
+---------------------+
11 rows in set

mysql> select concat(path,"-",id) from tb_kind;
+---------------------+
| concat(path,"-",id) |
+---------------------+
| 0-1 |
| 0-2 |
| 0-3 |
| 0-4 |
| 0-1-5 |
| 0-1-6 |
| 0-1-7 |
| 0-1-5-8 |
| 0-1-5-9 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
+---------------------+
11 rows in set

mysql> select concat(path,"-",id) as abs from tb_kind order by abs.path;
ERROR 1054 : Unknown column 'abs.path' in 'order clause'
mysql> select concat(path,"-",id) as abs from tb_kind order by abs

+------------+
| abs |
+------------+
| 0-1 |
| 0-1-5 |
| 0-1-5-8 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
| 0-1-5-9 |
| 0-1-6 |
| 0-1-7 |
| 0-2 |
| 0-3 |
| 0-4 |
+------------+
11 rows in set
mysql> select concat(path,"-",id) as,id,name,path abs from tb_kind order by abs;
ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id,name,path abs from tb_kind order by abs' at line 1
mysql> select concat(path,"-",id) as abs,
id,pname,path abs from tb_kind order by abs;
+------------+----+----------+---------+
| abs | id | pname | abs |
+------------+----+----------+---------+
| 0-1 | 1 | 新闻 | 0 |
| 0-1-5 | 5 | 体育新闻 | 0-1 |
| 0-1-5-8 | 8 | 篮球新闻 | 0-1-5 |
| 0-1-5-8-10 | 10 | NBA | 0-1-5-8 |
| 0-1-5-8-11 | 11 | CBA | 0-1-5-8 |
| 0-1-5-9 | 9 | 足球新闻 | 0-1-5 |
| 0-1-6 | 6 | 娱乐新闻 | 0-1 |
| 0-1-7 | 7 | 财经新闻 | 0-1 |
| 0-2 | 2 | 视频 | 0 |
| 0-3 | 3 | 图片 | 0 |
| 0-4 | 4 | 博客 | 0 |
+------------+----+----------+---------+
11 rows in set
mysql>

php处理分类源码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<!--显示结果
新闻
体育新闻
篮球新闻
NBA
CBA
足球新闻
娱乐新闻
财经新闻
视频
图片
博客
-->
<?
$conn=mysql_connect("localhost","root","root");
mysql_select_db("db_kind");
mysql_query("set names utf8");
$sql="select concat(path,'-',id) as abspath,id,pname,path from tb_kind order by abspath";
$rs=mysql_query($sql);
while($result=mysql_fetch_assoc($rs)){
$num=count(explode("-",$result[path]))-1;
$new_str=str_repeat("---",$num);
echo $new_str.$result[pname];
echo "<br>";
}
$str=str_repeat("=",10);
echo $str;
$num=count(explode("-","0-1-5-8"))-1;
echo $num;
?>
</body>
</html>

这里统计了php中大量的四舍五入函数,包括有round(),ceil(),floor()等,有需要的朋友参考一下。
 代码如下 复制代码
<?php
  $s = rand(100,200);  
  $pi=pi();
  $r=sqrt($s/$pi);
  $qz1=round($r);   //四舍五入取整
  $qz2=ceil($r);    //进一法取整
  $qz3=floor($r);   //舍去法取整
 
  echo "随机产生的圆的面积为:".$s."<br>";
  echo "通过除法和开方计算出的圆的半径为:".$r."<br>";
  echo "四舍五入取整后:".$qz1."<br>";
  echo "进一法取整后:".$qz2."<br>";
  echo "舍去法取整后:".$qz3."<br>";
 ?>
这是自己开发用到的一个简单的购物车功能的php代码,用了几个文件没用数据库就实现了购物车这样做如果用户关了浏览器,购物车里的商品就会全部丢失哦,有需要的朋友可以改进一下,利用数据库+session +cookie来实现会很好一些。
 代码如下 复制代码

<?php
class Shopcar
{
//商品列表
public  $productList=array();

/**
 *
 * @param unknown_type $product 传进来的商品
 * @return true 购物车里面没有该商品
 */
public function checkProduct($product)
{
 
 for($i=0;$i<count($this->productList);$i++ )
 {
 
  if($this->productList[$i]['name']==$product['name'])  
  return $i;
 }
 
 return -1;

}
 //添加到购物车
 public function add($product)
{
 $i=$this->checkProduct($product);
 if($i==-1)
 array_push($this->productList,$product);
 else
 $this->productList[$i]['num']+=$product['num']; 
}
//删除
public function delete($product)
{
 $i=$this->checkProduct($product);
 if($i!=-1)
 array_splice($this->productList,$i,1);
 
}

//返回所有的商品的信息
public function show()
{
  return $this->productList;

}


}

html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript"" width=100% src='jquery.min.js'></script>
<script type="text/javascript">
function buy(i)
{
var num=$(':input[name=num]')[i].value;
var name=$('[name=name]')[i].innerHTML;
var price=$('[name=price]')[i].innerHTML;

alert(num+name+price);
$.ajax({
  type:'post',                    //传送的方式,get/post
  url:'index.php',   //发送数据的地址
  cache:'false',
  data:'num='+num+"&name="+name+"&price="+price,            
  success:function(data)
  {
  alert(data);
  }
})

 

}


</script>
</head>
<body>

<table>
<tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td><td>购买</td></tr>

<tr><td>0</td><td><label name='name' >商品1</label></td><td><label name='price'>1</label>
</td><td><input  name='num' type='text' value='1' /></td><td><a  onclick='buy(0)'><u><font color='blue'>购买</font></u></a></td></tr>

<tr><td>1</td><td><label name='name' >商品2</label></td><td><label name='price'>2</label>
</td><td><input  name='num' type='text' value='1' /></td><td><a  onclick='buy(1)'>购买</a></td></tr>

<tr><td>2</td><td><label name='name' >商品3</label></td><td><label name='price'>1</label>
</td><td><input  name='num' type='text' value='1' /></td><td><a  onclick='buy(2)'>购买</a></td></tr>
<tr><td>3</td><td><label name='name' >商品4</label></td><td><label name='price'>1</label>
</td><td><input  name='num' type='text' value='1' /></td><td><a  onclick='buy(3)'>购买</a></td></tr>

<tr><a href='show.php'>查看购物车</a></tr>

 


</table>

</body>
</html>
index.ph

<?php
require 'Shopcar.class.php';
session_start();

$name=$_POST['name'];
$num=$_POST['num'];
$price=$_POST['price'];
$product=array('name'=>$name,'num'=>$num,'price'=>$price);
print_r($product);
if(isset($_SESSION['shopcar']))
$shopcar=unserialize($_SESSION['shopcar']);
else
$shopcar=new Shopcar();
$shopcar->add($product);
$_SESSION['shopcar']=serialize($shopcar);

show.php

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<table>
<tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td></tr>
<?php
require 'Shopcar.class.php';
session_start();
$shopcar=unserialize($_SESSION['shopcar']);

print_r($shopcar);
$productList=$shopcar->productList;

foreach ($productList as $product){
?>
<tr><td>1</td><td><label ><?php echo $product['name']?></label></td><td><label name='price'><?php echo $product['price']?></label>
</td><td><input  name='num' type='text' value='<?php echo $product['num']?>' /></td></tr>
<?php }?>
</table>
</body>
</html>

标签:[!--infotagslink--]

您可能感兴趣的文章: