Heim  >  Artikel  >  Backend-Entwicklung  >  PHP 字符串分割和比较_PHP教程

PHP 字符串分割和比较_PHP教程

WBOY
WBOYOriginal
2016-07-21 15:43:58818Durchsuche

或者使用strcmp来判断,但是这个能够告诉你两个字符串是否相等,但是无法告诉你在那里不同。
我的思路是单字符串分割为一个个字母(character),这样比较就能精确知道在那个位置不同了。
分隔字符串,使用“str_split”就可以了,语法参考【2】。然后输出结果数组,好处是连空格也会作为数组的元素。我之前的例子就是因为前一个字符串包含2个空格,而后一个只有一个。但是输出的时候看到的显示都是一样的。
也可以按照其他分隔符进行分割,如“explode”或者“preg_split”,参考【3】和【4】。前者使用简单的分割数,后者使用正则表达式。“split”已经废除(deprecated)了。
还有一个很有用的函数就是“str_word_count”,可以将字符串分割为数组,同时清除数字和标点符号。如果你想统计单词出现的频率,就可使用“array_count_values”,参考【5】。
参考:
【1】http://us2.php.net/manual/en/language.operators.comparison.php
【2】http://us2.php.net/manual/en/function.str-split.php1
【3】http://us2.php.net/manual/en/function.explode.php
【4】http://us2.php.net/manual/en/function.str-split.php
【5】http://us2.php.net/manual/en/function.array-count-values.php

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/320610.htmlTechArticle或者使用strcmp来判断,但是这个能够告诉你两个字符串是否相等,但是无法告诉你在那里不同。 我的思路是单字符串分割为一个个字母(ch...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn