Home > Backend Development > PHP Tutorial > PHP learning series (1) - string processing function (3), php function_PHP tutorial

PHP learning series (1) - string processing function (3), php function_PHP tutorial

WBOY
Release: 2016-07-13 10:17:33
Original
1199 people have browsed it

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>?>
Copy after login

输出:

Without %u: 461707669
With %u: 461707669
Copy after login
例子 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>?>
Copy after login

输出:

Without %u: -1959132156
With %u: 2335835140
Copy after login

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&lt;br /&gt;"<span>;
}
</span><span>else</span><span>
{
</span><span>echo</span> "Standard DES not supported.\n&lt;br /&gt;"<span>;
}

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

</span><span>if</span> (CRYPT_MD5 == 1<span>)
{
</span><span>echo</span> "MD5: ".<span>crypt</span>("hello world")."\n&lt;br /&gt;"<span>;
}
</span><span>else</span><span>
{
</span><span>echo</span> "MD5 not supported.\n&lt;br /&gt;"<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>
Copy after login

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

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

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

<?<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>?>
Copy after login

输出:

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

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

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

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>;
?>
Copy after login

输出:

27
Copy after login

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

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

输出:

123.000000
Copy after login
例子 3

使用占位符:

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

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

With 2 decimals: 123.00
With no decimals: 123
Copy after login

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

语法:hebrev(string,maxcharline)

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

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

<br /> 
Copy after login

php 怎处理字符串

By learning PHP, you can use this high-level language to create a website with higher performance. For beginners, the PHP string mbstring is still relatively unfamiliar. Let's introduce the specific application of PHP string mbstring.

The coexistence of multiple languages ​​means multiple bytes. PHP's built-in string length function strlen cannot correctly handle Chinese strings. It only gets the number of bytes occupied by the string. For GB2312 Chinese encoding, the value obtained by strlen is twice the number of Chinese characters, while for UTF-8 encoded Chinese, the difference is 1 to 3 times.

Using PHP string mbstring can better solve this problem. The usage of mb_strlen is similar to strlen, except that it has a second optional parameter to specify the character encoding. For example, to get the length of the UTF-8 string $str, you can use mb_strlen($str,’UTF-8′). If the second parameter is omitted, PHP's internal encoding will be used. The internal encoding can be obtained through the mb_internal_encoding() function. There are two ways to set it:

1. Set mbstring.internal_encoding = UTF-8 in php.ini

2. Call mb_internal_encoding("GBK ”)

In addition to the PHP string mbstring, there are many cutting functions, among which mb_substr splits characters by words, and mb_strcut splits characters by bytes, but neither of them produces half a character. Phenomenon. Moreover, cutting from functions has different effects on length. The cutting condition of mb_strcut is less than strlen, and mb_substr is equal to strlen. See the example below,

< ? $str = 'I am a relatively long string of Chinese characters -www.jefflei.com'; echo “mb_substr:” . mb_substr($str, 0, 6, 'utf-8′); echo ” “; echo “mb_strcut:” . mb_strcut($str, 0, 6, ' utf-8′); ?>

The output is as follows:

mb_substr: I am a string of comparisons

mb_strcut: I am

Need to pay attention Yes, PHP string mbstring is not a core function of PHP. Before using it, you need to make sure to add mbstring support when compiling the module in PHP:

(1) Use –enable-mbstring

(2) when compiling )Modify /usr/local/lib/php.inc

default_charset = “zh-cn”

mbstring.language = zh-cn

mbstring.internal_encoding =zh- cn

The PHP string mbstring class library has a lot of content, and also includes email processing functions such as mb_ send_ mail, etc.

Is there a direct function for adding 1 to a php string? Or code writing

I would like to point out that the poster’s need to “perform arithmetic operations on strings” is quite distorted.
Even if it is implemented, it is very low from the perspective of performance efficiency. I personally don't see any practical or theoretical significance.
can be seen as 2 parts
letter part, which is hexadecimal (if the character set includes Chinese, it will become tens of thousands of decimals), a=1...z=26 has z+a=26+1= aa accepts decimal addition input
The digital part is decimal, 9+1=10,

and there is still a lack of explanation on "Is a carry after a999+1? How to carry?"
There are multiple possible results
1. No carry. a999+1 is a000,
2. Only decimal partial carry. a999+1 gets a1000
2. Overall carry. a999+1 gets b000

You need to explain this before you can write the implementation

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/890821.htmlTechArticlePHP learning series (1) - string processing function (3), php function 11, crc32() function Computes the crc32 polynomial of a string. Generate a 32-bit cyclic redundancy check code for the string parameter...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template