search
Homephp教程php手册PHP学习系列(1)字符串处理函数(3),php函数

PHP学习系列(1)——字符串处理函数(3),php函数

11、crc32() 函数计算一个字符串的 crc32 多项式。生成 string 参数的 32 位循环冗余校验码多项式。该函数可用于验证数据的完整性。

语法:crc32(string)

注意:由于 PHP 的整数是带符号的,许多 crc32 校验码将返回负整数,因此您需要使用 sprintf() 或 printf() 的 "%u" 格式符来获取表示无符号 crc32 校验码的字符串。

例子 1

在本例中,我们将在使用以及不使用 "%u" 格式符的情况下,输出 crc32() 的结果(注意结果是相同的):

<?<span>php
</span><span>$str</span> = <span>crc32</span>("Hello world!"<span>);
</span><span>echo</span> 'Without %u: '.<span>$str</span>."<br />"<span>;
</span><span>echo</span> 'With %u: '<span>;
</span><span>printf</span>("%u",<span>$str</span><span>);
</span>?>

输出:

Without %u: 461707669
With %u: 461707669
例子 2

在本例中,我们将在使用以及不使用 "%u" 格式符的情况下,输出 crc32() 的结果(注意结果是不相同的):

<?<span>php
</span><span>$str</span> = <span>crc32</span>("Hello world."<span>);
</span><span>echo</span> 'Without %u: '.<span>$str</span>."<br />"<span>;
</span><span>echo</span> 'With %u: '<span>;
</span><span>printf</span>("%u",<span>$str</span><span>);
</span>?>

输出:

Without %u: -1959132156
With %u: 2335835140

12、crypt() 函数返回使用 DES、Blowfish 或 MD5 加密的字符串。在不同的操作系统上,本函数的行为不同,某些操作系统支持一种以上的算法类型。在安装时,PHP 会检查什么算法可用以及使用什么算法。

语法:crypt(str,salt)

salt参数可选。用于增加被编码字符数目的字符串,以使编码更加安全。如果未提供 salt 参数,则每次调用该函数时会随机生成一个。

确切的算法依赖于 salt 参数的格式和长度。

下面是与 crypt() 函数一起使用的一些常量。在安装时,由 PHP 设置这些常量:

  • [CRYPT_SALT_LENGTH]
  • [CRYPT_STD_DES]
  • [CRYPT_EXT_DES]
  • [CRYPT_MD5]
  • [CRYPT_BLOWFISH]

注意:解密算法是没有的,这是一种单向加密方法

在本例中,我们将测试不同的算法:

<?<span>php
</span><span>if</span> (CRYPT_STD_DES == 1<span>)
{
</span><span>echo</span> "Standard DES: ".<span>crypt</span>("hello world")."\n<br />"<span>;
}
</span><span>else</span><span>
{
</span><span>echo</span> "Standard DES not supported.\n<br />"<span>;
}

</span><span>if</span> (CRYPT_EXT_DES == 1<span>)
{
</span><span>echo</span> "Extended DES: ".<span>crypt</span>("hello world")."\n<br />"<span>;
}
</span><span>else</span><span>
{
</span><span>echo</span> "Extended DES not supported.\n<br />"<span>;
}

</span><span>if</span> (CRYPT_MD5 == 1<span>)
{
</span><span>echo</span> "MD5: ".<span>crypt</span>("hello world")."\n<br />"<span>;
}
</span><span>else</span><span>
{
</span><span>echo</span> "MD5 not supported.\n<br />"<span>;
}

</span><span>if</span> (CRYPT_BLOWFISH == 1<span>)
{
</span><span>echo</span> "Blowfish: ".<span>crypt</span>("hello world"<span>);
}
</span><span>else</span><span>
{
</span><span>echo</span> "Blowfish DES not supported."<span>;
}
?></span>

输出类似(依赖于操作系统):

Standard DES: $1$r35.Y52.$iyiFuvM.zFGsscpU0aZ4e. 
Extended DES not supported. 
MD5: $1$BN1.0I2.$8oBI/4mufxK6Tq89M12mk/ 
Blowfish DES not supported.
13、explode() 函数把字符串分割为数组。
语法:explode(separator,string,limit)
说明:本函数返回由字符串组成的数组,其中的每个元素都是由 separator 作为边界点分割出来的子字符串。
separator 参数不能是空字符串。如果 separator 为空字符串(""),explode() 将返回 FALSE。
如果 separator 所包含的值在string 中找不到,那么 explode() 将返回包含 string 中单个元素的数组。如果设置了 limit 参数,
则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。如果 limit 参数是负数,则返回除了最后的 -limit 个元素外的所有元素。
此特性是 PHP 5.1.0 中新增的。
注意:参数 limit 是在 PHP 4.0.1 中加入的。由于历史原因,虽然 implode() 可以接收两种参数顺序,但是 explode() 不行。
你必须保证 <em>separator</em> 参数在 <em>string</em> 参数之前才行。

例子:在本例中,我们将把字符串分割为数组:

<?<span>php
</span><span>$str</span> = "Hello world. It's a beautiful day."<span>;
</span><span>print_r</span> (<span>explode</span>(" ",<span>$str</span><span>));
</span>?>

输出:

Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
 
14、fprintf() 函数把格式化的字符串写到指定的输出流(例如:文件或数据库)。

该函数返回被写字符串的长度。

语法
fprintf(stream,format,arg1,arg2,arg++)

stream——可选。规定在哪里写/输出字符串。

format——必需。转换格式。

arg1——必需。规定插到 format 字符串中第一个 % 符号处的参数。

arg2——可选。规定插到 format 字符串中第二个 % 符号处的参数。

arg++——可选。规定插到 format 字符串中第三、四等等 % 符号处的参数。

说明:参数 format 是转换的格式,以百分比符号 ("%") 开始到转换字符结束。下面的可能的 format 值:
  • %% - 返回百分比符号
  • %b - 二进制数
  • %c - 依照 ASCII 值的字符
  • %d - 带符号十进制数
  • %e - 可续计数法(比如 1.5e+3)
  • %u - 无符号十进制数
  • %f - 浮点数(local settings aware)
  • %F - 浮点数(not local settings aware)
  • %o - 八进制数
  • %s - 字符串
  • %x - 十六进制数(小写字母)
  • %X - 十六进制数(大写字母)

arg1, arg2, ++ 等参数将插入到主字符串中的百分号 (%) 符号处。该函数是逐步执行的。在第一个 % 符号中,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。

提示和注释

注释:如果 % 符号多于 arg 参数,则您必须使用占位符。占位符被插入 % 符号之后,由数字和 "\$" 组成。请参见例子 3。

提示: 相关函数: printf()、 sprintf()、 vfprintf()、 vprintf() 以及 vsprintf()。

例子

例子 1
<?php
$str = "Hello";
$number = 123;
$file = fopen("test.txt","w");
echo <code>fprintf($file,"%s world. Day number %u",$str,$number)</code>;
?>

输出:

27

以下文本将写入 "test.txt":

Hello world. Day number 123
例子 2
<?php
$number = 123;
$file = fopen("test.txt","w");
<code>fprintf($file,"%f",$number);</code>
?>

输出:

123.000000
例子 3

使用占位符:

<?php
$number = 123;
$file = fopen("test.txt","w");
<code>fprintf($file,"With 2 decimals: %1\$.2f\nWith no decimals: %1\$u",$number)</code>;
?>

以下文本将写入 "test.txt":

With 2 decimals: 123.00
With no decimals: 123

15、hebrev() 函数把希伯来文本从右至左的流转换为左至右的流。只有 224 至 251 之间的 ASCII 字符,以及标点符号受到影响。

语法:hebrev(string,maxcharline)

maxcharline——规定每行的最大字符数。如果可能,hebrev() 将避免把单词断开。

说明:hebrev() 和 hebrevc() 可以把希伯来逻辑文本转换为希伯来可见文本。希伯来可见文本不需要特殊的右至左字符支持,这使它对于在 web 上显示希伯来文本很有用处。

<br /> 

php 怎处理字符串

大家通过对PHP的学习,可以运用这一高级语言创建一个性能较高的网站。对于初学者来说,对于PHP字符串mbstring还是比较陌生的,下面我们就来介绍一下PHP字符串mbstring的具体应用。

多国语言并存就意味着多字节,PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数。对于GB2312的中文编码,strlen得到的值是汉字个数的2倍,而对于UTF-8编码的中文,就是1~3倍的差异了。

采用PHP字符串mbstring可以较好地解决这个问题。mb_strlen的用法和strlen类似,只不过它有第二个可选参数用于指定字符编码。例如得到UTF-8的字符串$str长度,可以用mb_strlen($str,’UTF-8′)。如果省略第二个参数,则会使用PHP的内部编码。内部编码可以通过mb_internal_encoding()函数得到,设置有两种方式:

1. 在php.ini中设置mbstring.internal_encoding = UTF-8

2. 调用mb_internal_encoding(”GBK”)

除了PHP字符串mbstring,还有很多切割函数,其中mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象。而且从函数切割对长度的作用也不同,mb_strcut的切割条件是小于strlen, mb_substr是等于strlen,看下面的例子,



输出如下:

mb_substr:我是一串比较

mb_strcut:我是

需要注意的是,PHP字符串mbstring并不是PHP核心函数,使用前需要确保在php编译模块时加入mbstring的支持:

(1)编译时使用–enable-mbstring

(2)修改/usr/local/lib/php.inc

default_charset = “zh-cn”

mbstring.language = zh-cn

mbstring.internal_encoding =zh-cn

PHP字符串mbstring类库内容比较多,还包括mb_ send_ mail 之类的email处理函数等
 

php 字符串加1 是否有直接函数?或代码写法

想指出的是楼主这种“在字符串上做算术运算”的需求是比较扭曲的,
即使实现,从性能效率的角度是非常低下。个人没有看出任何实用和理论意义。
可以看成2部分
字母部分,为26进制(如果字符集包括中文将成为几万进制),a=1...z=26 有z+a=26+1=aa 接受10进制加法输入
数字部分,为10进制,9+1=10,

而且还缺少对“a999+1后是否进位?怎么进位?”进行说明
结果有多个可能
1、不进位。a999+1得a000 ,
2、仅10进制部分进位。a999+1得a1000
2、整体进位。a999+1得b000

需要说明这点后才可能写出实现
 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么将16进制字符串转为数字php怎么将16进制字符串转为数字Oct 26, 2021 pm 06:36 PM

php将16进制字符串转为数字的方法:1、使用hexdec()函数,语法“hexdec(十六进制字符串)”;2、使用base_convert()函数,语法“bindec(十六进制字符串, 16, 10)”。

php怎么将字符串转换成小数php怎么将字符串转换成小数Mar 22, 2023 pm 03:22 PM

PHP 是一门功能强大的编程语言,广泛应用于 Web 开发领域。其中一个非常常见的情况是需要将字符串转换为小数。这在进行数据处理的时候非常有用。在本文中,我们将介绍如何在 PHP 中将字符串转换为小数。

golang怎么检测变量是否为字符串golang怎么检测变量是否为字符串Jan 06, 2023 pm 12:41 PM

检测变量是否为字符串的方法:1、利用​“%T”格式化标识,语法“fmt.Printf("variable count=%v is of type %T \n", count, count)”;2、利用reflect.TypeOf(),语法“reflect.TypeOf(变量)”;3、利用reflect.ValueOf().Kind()检测;4、使用类型断言,可以对类型进行分组。

php 字符串长度不一致怎么办php 字符串长度不一致怎么办Feb 07, 2023 am 09:58 AM

php字符串长度不一致的解决办法:1、通过mb_detect_encoding()函数查看字符串的编码方式;2、通过mb_strlen函数查看具体字符长度;3、使用正则表达式“preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str1, $matches);”剔除非中文字符即可。

go语言怎么删除字符串中的空格go语言怎么删除字符串中的空格Jan 17, 2023 pm 02:31 PM

删除方法:1、使用TrimSpace()函数去除字符串左右两边的空格,语法“strings.TrimSpace(str)”;2、使用Trim()函数去除字符串左右两边的空格,语法“strings.Trim(str, " ")”;3、使用Replace()函数去除字符串的全部空格,语法“strings.Replace(str, " ", "", -1)”。

php字符串函数学习:怎么去掉前面的字符php字符串函数学习:怎么去掉前面的字符Mar 20, 2023 pm 02:33 PM

在开发PHP应用程序时,有时我们需要去掉字符串前面的某些特定字符或者字符串。在这种情况下,我们需要使用一些PHP函数来实现这一目标。本文将介绍一些PHP函数,帮助您轻松地去掉字符串前面的字符或字符串。

php怎么将字符串转为布尔类型php怎么将字符串转为布尔类型Jul 01, 2021 pm 06:36 PM

转换方法:1、在转换变量前加上用括号括起来的目标类型“(bool)”或“(boolean)”;2、用boolval()函数,语法“boolval(字符串)”;3、用settype()函数,语法“settype(变量,"boolean")”。

php字符串部分乱码怎么办php字符串部分乱码怎么办Jan 20, 2023 am 10:18 AM

php字符串部分乱码的解决办法:1、使用“mb_substr(strip_tags($str),0,-1,'UTF-8');”截取字符串;2、使用“iconv("UTF-8","GB2312//IGNORE",$data)”转换字符集即可。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment