未加星标

Useful String Functions in PHP

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
php provides a long list of string functions that can be used to perform actions on strings. Explaining all of the PHP string functions is beyond the scope of this post, but I can surely take you through the commonly used string functions.

In this post, I’m going to explain the most useful string functions in PHP with code examples.

Change the 1st character of a string to lowercase

The Icfirst() function converts the first character of a string to lowercase. The code example below converts the first character of the string “Excellent work” to lowercase. So the string “Excellent work” becomes “excellent work”.

Code example:

Output: excellent work Similar function: ucfirst(): This function changes the first character of a string to uppercase.

Change the 1st character of every word in a string to uppercase

The ucwords() function converts the first character of every word in a string to uppercase. The code example below changes the string “happy new year” to “Happy New Year”.

Code example:

Output:

Happy New Year

Change the whole string to lowercase

The strtolower() function converts the whole string to lowercase. The code example below changes the string “GOOD MORNING” to “good morning”.

Code example:

Output: good morning Similar function: strtoupper(): This function changes the whole string to uppercase.

Determine the length of a string The strlen() function gives you the length of a string. The code example below calculates the length of the string “JOHN”. Code example:

Output:

4

Reverse a string

The strrev() function reverses a string. The code example below reverses the string “John”.

Code example:

Output:nhoJ Count the number of words in a string The str_word_count() function returns the number of words in a string. The code example below displays the number of words present in the string “Currently it is 2017”.

Code example:

Output:

4

Count how many times a substring appears in a string

The substr_count() function counts how many times a substring appears in a given string. It takes two string arguments. The first argument is the main string and the second one is the substring that needs to be counted. Note that this is a case-sensitive function.

The code example below counts how many times the substring “John” appears in the string “John is a nice person. John loves PHP”.

Code example:

Output:2 Repeat a string The str_repeat() function repeats a given string for as many times as you want. It takes two arguments. The first argument is the string that you want to repeat, and the second one is the number of times you want to repeat it. The code example below shows the string “Hello” 2 times.

Code example:

Output:HelloHello

Trim a string from left

The ltrim() function trims a string from left. More specifically, it removes whitespaces and/or the specified characters from the left side of the string. This function takes two arguments. The first argument is the string that needs to be trimmed while the second one is the set of characters that need to be removed from the string.

Note that the second argument is optional. If you omit it, characters such as NULL, whitespaces, tab, newline, etc. will be removed from the left side of the string.

Code example:

Output: love coding in PHP Similar functions: rtrim(): This function trims a string from the right side. trim(): This function trims a string from the left and right sides.

Compare two strings

strcasecmp() is a case-insensitive function that compares two strings. This function returns 0 if the strings are equal. It returns a positive value if the first string is greater than the second string and returns a negative value if the first string is less than the second string. The code example below displays 0 as the strings “Programmers are magicians” and “PROGRAMMERS ARE MAGICIANS” are equal.

Code example:

Output:

0

Similar function: strcmp(): This is a case-sensitive function that compares two strings.

Extract a substring from a string

The substr() function extracts a substring from a string. It takes two arguments. The first argument is the string from which you want to extract a substring while the second argument is the number that specifies the position from where you want to begin extracting.

The code example below extracts a substring from the 4th position of the string “GODISGREAT”.

Code example:

Output:ISGREAT

Replace characters in a string str_ireplace() is a case-insensitive function that replaces a specified set of characters with another specified set of characters in a string. It takes three string arguments. The first argument specifies the set of characters that you want to replace. The second argument is the set of characters with which you want to replace. And the third argument is the string in which you want to replace. The following code example replaces “NEW” with “no” in the string “new beginning”.

Code example:

no beginning

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHPGRESGExcel
分页:12
转载请注明
本文标题:Useful String Functions in PHP
本站链接:http://www.codesec.net/view/520500.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(71)