您的位置 首页 php

PHP常用字符串函数(1),PHP面试重点

获取字符串长度

1、strlen ( string $string ) : int

获取字符串长度

$str = 'abc';
echo strlen($str);//输出3
$str = 'a bc';
echo strlen($str);//输出4,空格也算一个字符
$str = "a\tbc";
echo strlen($str);//输出4,空格也算一个字符,\t是 转义字符 ,算一个字符
$str = "a\r\nbc";
echo strlen($str);//输出5,\r和\n都是转义字符,都是一个字符,
$str = "a
bc";
echo strlen($str);//输出5,换行是\r\n,\r和\n都是转义字符,都是一个字符,
$str = "极客时光机";
echo strlen($str);//输出15,在utf-8下,一个汉字长度算3,ANSI格式下显示2 

2、mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] ) : mixed

参数说明:

str要检查长度的字符串。

encodingencoding 参数为 字符编码 。如果省略,则使用内部字符编码。

$str = "极客时光机";
echo mb_strlen($str);//15 

字符串查找函数

1、strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int

查找字符串首次出现的位置,返回 needle 在 haystack 中首次出现的数字位置。haystack是草垛,needle是针,就是在草垛中寻找针。

参数说明:

haystack在该字符串中进行查找。

needle如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符的顺序值。

offset如果提供了此参数,搜索会从字符串该字符数的起始位置开始统计。 如果是负数,搜索会从字符串结尾指定字符数开始。

返回值:返回 needle 存在于 haystack 字符串起始的位置(独立于 offset)。同时注意字符串位置是从0开始,而不是从1开始的。如果没找到 needle,将返回 FALSE

2、stripos ( string $haystack , string $needle [, int $offset = 0 ] ) : int

和strpos()唯一不同的是忽略大小写。函数名称多了个“i”,即ignore。

3、strrpos ( string $haystack , string $needle [, int $offset = 0 ] ) : int

计算指定字符串在目标字符串中最后一次出现的位置。

4、strripos ( string $haystack , string $needle [, int $offset = 0 ] ) : int

计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写),和strrpos()不同

5、strstr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string

返回从 needle 第一次出现的位置开始到 haystack 结尾的字符串。

参数说明:

haystack输入字符串。

needle如果 needle 不是一个字符串,那么它将被转化为整型并且作为字符的序号来使用。

before_needle若为 TRUE,strstr() 将返回 needle 在 haystack 中的位置之前的部分。

返回值:返回字符串的一部分,没有要查找的字符串则返回 FALSE。

6、strchr

strstr()函数的别名。

7、stristr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string

和strstr()不同的是,该函数忽略大小写。

8、strrchr ( string $haystack , mixed $needle ) : string

查找指定字符在字符串中的最后一次出现。该函数返回从needle 的最后出现位置开始,到 haystack 末尾。

返回值:该函数返回字符串的一部分。如果 needle 未被找到,返回 FALSE。

字符串替换函数(查找并替换)

1、str_ replace ( mixed $search , mixed $replace , mixed $ subject [, int &$count ] ) : mixed

在subject中查找search,全部用replace替换,返回替换后的字符串。

参数说明:

如果 search 和 replace 为数组,那么 str_replace() 将对 subject 做二者的映射替换。如果 replace 的值的个数少于 search 的个数,多余的替换将使用空字符串来进行。如果 search 是一个数组而 replace 是一个字符串,那么 search 中每个元素的替换将始终使用这个字符串。该转换不会改变大小写。

如果 search 和 replace 都是数组,它们的值将会被依次处理。

search查找的目标值,也就是 needle。一个数组可以指定多个目标。

replacesearch 的替换值。一个数组可以被用来指定多重替换。

subject执行替换的数组或者字符串。也就是 haystack。

如果 subject 是一个数组,替换操作将遍历整个 subject,返回值也将是一个数组。

count如果被指定,它的值将被设置为替换发生的次数。

返回值:返回替换后的数组或者字符串。

2、str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed

和str_replace()不同的是,该函数忽略大小写。

字符串大小写转换函数

1、strtolower ( string $string ) : string

将 string 中所有的字母字符转换为小写并返回。

2、strtoupper ( string $string ) : string

将 string 中所有的字母字符转换为大写并返回。

3、ucwords ( string $str [, string $delimiters = ” \t\r\n\f\v” ] ) : string

将字符串中每个单词的首字母转换为大写。

str输入字符串。

delimiters可选的,单词分割字符,默认分隔符为空格符。

4、ucfirst ( string $str ) : string

将字符串的首字母转换为大写,返回转换后的字符串。

5、lcfirst ( string $str ) : string

将字符串的首字母转换为小写,返回转换后的字符串。

字符串截取函数

1、substr ( string $string , int $start [, int $length ] ) : string

截取字符串,返回截取后的字符串。

参数说明:

(1)string输入字符串。必须至少有一个字符。

(2)start如果 start 是非负数,返回的字符串将从 string 的 start 位置开始,从 0 开始计算。

如果 start 是负数,返回的字符串将从 string 结尾处向前数第 start 个字符开始。

如果 string 的长度小于 start,将返回 FALSE。

(3)length如果提供了正数的 length,返回的字符串将从 start 处开始最多包括 length 个字符(取决于 string 的长度)。

如果提供了负数的 length,那么 string 末尾处的 length 个字符将会被省略(若 start 是负数则从字符串尾部算起)。如果 start 不在这段文本中,那么将返回 FALSE。

如果提供了值为 0,FALSE 或 NULL 的 length,那么将返回一个空字符串。

如果没有提供 length,返回的子字符串将从 start 位置开始直到字符串结尾。

返回值:返回提取的子字符串, 或者在失败时返回 FALSE。

2、substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) : mixed

替换字符串的子字符串。将字符串从start位置开始的子字符串替换为replacement。

(1)string输入字符串。

(2)replacement替换的字符串。

(3)start如果 start 为正数,替换将从 string 的 start 位置开始。

如果 start 为负数,替换将从 string 的倒数第 start 个位置开始。

(4)length如果设定了这个参数并且为正数,表示 string 中被替换的子字符串的长度。如果设定为负数,它表示待替换的子字符串结尾处距离 string 末端的字符个数。如果没有提供此参数,那么它默认为 strlen( string ) (字符串的长度)。当然,如果 length 为 0,那么这个函数的功能为将 replacement 插入到 string 的 start 位置处。

返回值:返回结果字符串。如果 string 是个数组,那么也将返回一个数组。

3、mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] ) : string

根据字符数执行一个多字节安全的 substr() 操作。

参数说明:

(1)str从该 string 中提取子字符串。

(2)start如果 start 不是负数,返回的字符串会从 str 第 start 的位置开始,从 0 开始计数。

如果 start 是负数,返回的字符串是从 str 末尾处第 start 个字符开始的。

lengthstr 中要使用的最大字符数。如果省略了此参数或者传入了 NULL,则会提取到字符串的尾部。

encodingencoding 参数为字符编码。如果省略,则使用内部字符编码。

返回值:返回子字符串。

文章来源:智云一二三科技

文章标题:PHP常用字符串函数(1),PHP面试重点

文章地址:https://www.zhihuclub.com/78497.shtml

关于作者: 智云科技

热门文章

评论已关闭

37条评论

  1. McGuiar s Plastic Polish 10 or 17 is available at most auto supply stores Look for the name and address of the pharmacy and make sure that it is connected to a reputable real-life pharmacy

  2. customs without any difficulty, but due to issues out of our control, they can be held up, misplaced or seized Sign electronically to place your order, or print your order form and fax back

  3. As is common to most people, brand-name Cialis comes in the form of tablets almond-shaped , which may be white, yellowish, or orange in color depending on their strengths, and oral jelly

  4. Additionally, consult your doctor about it or if any of the following side effects last for a long period of time. These results indicate that the autophagy inducer, Sert, can promote the degradation of MAPT proteins during proteotoxic stress.

  5. OUTLINE This is a randomized, double blind study An additional therapy would be the topical use of the shampoo Nizoral on the scalp, where its active ingredient Ketoconazole effectively blocks DHT from binding to receptors on the scalp or wherever it is rubbed on much like the relation between a SERM and Estrogen in breast tissue

  6. Or does he have other plans Anyway, no matter what it is, Zhao Ling is very interested in the matter of the scorpion, and he will try his best to find out her bastard in the back, but with their current lineup, there is no Knowing where the dead scorpion is hiding, if you do it at this time, it will easily cause a lot of unnecessary misunderstandings and troubles

  7. All of the women had been diagnosed between 1980 and 2000, and all were members of Kaiser Permanente, a large health maintenance organization

  8. These data indicate that fulvestrant resistant CAMA 1 and ZR 75 1 cells may have developed dependency on the CDK2 cyclin E axis for cell growth South Africa L Goedhals, L Smith Nationale Hospital, Bloemfontein, South Africa; I Werner, E Murray Groote Schuur Hospital, Cape Town, South Africa; D Hacking Durban Oncology Centre, Westridge, Durban, South Africa; D Vorobiof, M Chasen Sandton Oncology Centre, Sandton, Johannesburg, South Africa

  9. The Department continues to use all available tools to assess and manage national shortages when they happen Perhaps continuation of the atrasentan treatment for a longer time could prevent further vascular injury because it attenuated or eliminated other factors, such as, elevated BP, oxidative stress, and immune cell infiltration

  10. In the late 1980 s and early 1990 s there was enormous pressure placed on the steroid market by the FDA and Searle simply discontinued the line In this review, Kingwell et al summarize how cholesterol efflux dysfunction leads to atherosclerosis and vulnerable plaque formation, including inflammatory cell recruitment, foam cell formation, the development of a lipid necrotic core, and degradation of the fibrous cap

  11. This compound was shown to be an effective inhibitor of ER action in xenograft models of both tamoxifen sensitive and resistant breast cancers and yielded positive results in a heavily treated population of patients with breast cancer with endocrine treatment resistant disease 20

  12. serdexmethylphenidate dexmethylphenidate increases effects of citalopram by decreasing metabolism posted a racy photo of herself posing in a tummy baring top and light green cutout bikini bottoms

  13. My own senior pup loves her daily glucosamine chew, and I can tell it makes a difference There are very few prospective trials to guide therapy

  14. levitra bupropion wiki Wilson used that stolen time to spread even more evil Гў including getting one of his prison guards pregnant Гў prompting an outraged Garaufis to demand a Justice Department investigation of the federal pen that held him

  15. The results were essentially unchanged, which may have been due to underreporting of polycystic ovary syndrome in the Danish National Patient Register PLoS ONE 10 5 e0122655

  16. High fat, high calorie diet enhances mammary carcinogenesis and local inflammation in MMTV PyMT mouse model of breast cancer

  17. economy for as long as needed continued to shore up sentiment in global markets Friday despite growing concerns over the scale of the slowdown in China Rastogi GK, Malhotra MS, Srivastava MC, et al Study of the pituitary thyroid functions at high altitude in man

网站地图