字符串函数
函数 | 说明 |
---|
strlen(string) | 获取字符串的长度 |
str_word_count(string) | 获取字符串中单词个数 |
strrev(string) | 字符串逆序 |
strpos(string,find,start) | 在string中查找find,从start开始(可选) |
str_replace(find,replace,string,count) | 将string中的find替换为replace, count表示替换的次数(可选) |
示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<title>Hello World!</title>
</head>
<body>
<div class="well">
<?php
$greeting = "Hello world!";
# 获取字符串长度
echo "The length of \"$greeting\" is " . strlen($greeting) . "<br>";
# 获取单词个数
echo "The count of words of \"$greeting\" is " . str_word_count($greeting) . "<br>";
# 逆序
echo "Reverse \"$greeting\" we get \"" . strrev($greeting) . "\"<br>";
# 查找
echo "Position of \"world\" in \"$greeting\" is " . strpos($greeting, "world") . "<br>";
# 替换
echo "Repalce \"world\" of \"$greeting\" with \"work\" we get \"" . str_replace("world", "work", $greeting) . "\"<br>";
?>
</div>
</body>
</html>
查看运行结果