---
title: "PHP 不区分中英文截取utf8字符串"
author: "onestopweb"
date: "2017-09-24T16:45:06Z"
tags: []
categories: ["编程语言"]
slug: "php-utf8"
draft: false
lastmod: "2017-09-24T16:45:06Z"
id: 2394281
url: "https://onestopweb.iteye.com/blog/2394281"
---

```
<?php
header('Content-Type: text/html; charset=utf-8');
//截取utf8字符串
function utf8Substr($str='', $start=0, $len=10)
{
    return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start.'}'.
        '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
        '$1',$str);
}
echo utf8Substr('阅谁问君诵，水落清香浮。',8,2),PHP_EOL;
echo utf8Substr('吴者然onestopweb');
```

 

效果图：  
![](http://dl2.iteye.com/upload/attachment/0127/1170/b9ea7a18-21a6-39d8-a1cc-3b3076c23620.jpg)