PHP 内置支持几种数据类型。除此之外,PHP 还支持许多在处理某些数据时使用的函数。 PHP 字符串函数是一些用于操作字符串数据的函数。所有这些功能都是预定义的。需要安装任何插件。让我们看看一些 PHP 字符串函数。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
下面是一些字符串函数,并使用以下语法举例说明。
<?php echo func( "<data>" ); ?>
字符串函数使用起来很简单。这里我们将通过示例讨论如何在 PHP 编程中使用字符串函数。
这将返回一个在特定字符前面带有反斜杠的字符串
例如:
echo addcslashes ("Hello World!","W");
输出:
你好世界
这将返回预定义字符前面带有反斜杠的字符串
例如:
echo addcslashes('Hello "World" you');
输出:
你好“世界”。
将二进制数据转换为十六进制数据
例如:
echo bin2hex ("Hello");
输出:
48656c6c6f
从右端删除空格或任何预定义字符(如果指定)
例如:
echo chop ("WelcomeBack" , "Back");
输出:
欢迎
此 PHP 字符串函数返回指定 ASCII 值的字符。
例如:
echo char(52);
输出:
4
用于将字符串分割成更小的部分
例如:
echo chunk_split ($str, 2,", ");
输出:
我们,lc,om,e,
这将解码 uuencoded 字符串
例如:
echo convert_uudecode ("+22!L;W9E( %!( 4\"$`\n` ");
输出:
我喜欢 PHP!
convert_uuencode() 与convert_uudecode()相反
此 PHP 字符串函数输出有关字符串中字符数的数据。
例如:
echo count_chars ("Hello", 3);
输出:
你好
注: 整数值是模式,用于指定所需输出的类型计算字符串的 32 位循环冗余校验和(数学函数)。
例如:
crc32 ("Hello World!");
输出:
472456355
这将数组元素与指定的字符串连接
例如:
$array = array ('lastname', 'email', 'phone'); echo implode(",", $array);
输出:
姓氏、电子邮件、电话
注意: join() 也做同样的事情。它是 implode() 的别名。这会将一些预定义的字符转换为 HTML 实体,即它显示源。
例如:
$str = "I am <b>Bold</b>"; echo $str; => I am <strong>Bold</strong> echo htmlspecialchars($str);
输出:
我大胆
此 PHP 字符串函数删除字符串左侧的空格或预定义字符。
例如:
echo ltrim ("Just a sample", "Just");
输出:
样品
注意: rtrim() 从右侧执行类似的工作这将数字格式化为千位分组
例如:
echo number_format (1000000);
输出:
1,000,000
这只是输出字符串并且比 echo 慢
此外,打印不应该与 () 一起使用
例如:
print "Hello";
输出:
你好
这会计算字符串的 md5 哈希值
例如:
echo md5 ("Hello");
输出:
8b1a9953c4611296a827abf8c47804d7
这会将字符串分割成更小的字符串
例如:
$string = "This is to break a string"; $token = strtok ($string, " "); echo($token); => This To get all words of string, while ($token !== false){ echo "$token<br>"; $token = strtok(" "); }
输出:
这个
是
到
休息
字符串
This converts a string to uppercase characters
E.g.:
echo strupper ("Beautiful Day");
Output:
BEAUTIFUL DAY
Note: strlower() converts strings to all lowercase characters.This returns part of the string starting with the index specified
E.g.:
echo subst ("A Hot Day", 3);
Output:
ot Day
This PHP string function replaces a part of the string with the string specified.
E.g.:
echo substr_replace ("Hot", "Day",0);
Output:
Day
This wraps a string to a number of characters
E.g.:
echo wordwrap ("Hello World", 5, "\n");
Output:
Hello
World
This is used to determine the length of the string passed
E.g.:
echo strlen ("Hello");
Output:
5
This PHP string function is used to get the reverse of the string
E.g.:
echo strrev ("welcome");
Output:
emoclew
This returns the position of the first occurrence of a string inside a string
E.g.:
echo strops("There you go", "go");
Output:
11
This repeats a string specified number of times
E.g.:
echo str_repeat ('b', 5);
Output:
bbbbb
This PHP string function finds the specified word, replaces that with a specified word, and return the string
E.g.:
echo str_replace ("great", "wonderful", "have a great day");
Output:
have a wonderful day
This PHP string function inserts html line breaks in front of each new line of the string
E.g.:
echo nl2br ("Lets break \nthe sentence");
Output:
Lets break
the sentence
This calculates the similarity between two strings
E.g.:
echo similar_text ("Hello World","Great World");
Output:
7
This PHP string function writes a formatted string to a variable
E.g.:
echo sprintf ("There are %u wonders in the World",7);
Output:
There are 7 wonders in the World
This replaces characters in the string with specific characters. This function is case insensitive.
E.g.:
echo str_ireplace ("great", "WOW", "This is a great place");
Output:
This is a wow place
This randomly shuffles all characters in a string
E.g.:
echo str_shuffle("Hello World");
Output:
lloeWlHdro
This PHP string function returns the number of words in the given string
E.g.:
echo str_word_count ("a nice day");
Output:
3
This returns the number of characters before the specified character
echo strcspn ("Hello world!","w");
Output:
6
This function is used to pad to the right side of the string, a specified number of characters with the specified character
E.g.:
echo str_pad ("Hello", 10, ".");
Output:
Hello…..
This PHP string function returns the ASCII value of the first character of the string.
E.g.:
echo ord ("hello");
Output:
104
Find the first occurrence of a specified string within a string
E.g.:
echo strchr ("Hello world!", "world");
Output:
world!
This returns the number of characters found in the string that contains characters from the specified string.
E.g.:
echo strspn ("Hello world!", "Hl");
Output:
1
There are few more string functions available in PHP. The above string functions are commonly used functions in PHP for various requirements.
以上是PHP 字符串函数的详细内容。更多信息请关注PHP中文网其他相关文章!