PHP字符串切分(对比Python)
本文完整阅读约需 3 分钟,如时间较长请考虑收藏后慢慢阅读~
PHP字符串切分(按字节切分):
<?php
$engString = "this is a sentence";
$chnString = "这是一个中文句子";
echo substr($engString, 0, 11)."<br />";
echo substr($engString, 0, -3)."<br />";
echo substr($chnString, 0, 12)."<br />"; //使用UTF-8编码,一个汉字为三个字节,切割长度必须为3的倍数
//注意:substr()函数的第三个参数不是终止字节位,而是总长度
?>
Python字符串切分(按字切分):
engString = "this is a sentence"
chnString = "这是一个中文句子"
jpnString = "时代を越える想い"
engString_ripped = engString[0:3]
print(engString_ripped)
chnString_ripped = chnString[0:5]
print(chnString_ripped)
jpnString_ripped = jpnString[0:7]
print(jpnString_ripped)