PHP提供了以下截取函数:1. substr():从指定位置截取指定长度的子字符串;2. substr_replace():替换或插入指定位置的子字符串;3. strstr():查找第一个出现的子字符串;4. strrpos():查找最后一个出现的子字符串。

PHP 截取函数
PHP 提供了多种截取函数,用于从字符串中提取子字符串。以下是常用的截取函数:
substr()
substr() 函数从字符串中截取指定长度的子字符串。语法如下:
<code class="php">substr(string $string, int $start, int $length)</code>
其中:
-
$string是要截取的字符串。 -
$start是截取开始的位置。 -
$length是截取的长度。
substr_replace()
substr_replace() 函数在字符串中替换或插入子字符串。语法如下:
立即学习“PHP免费学习笔记(深入)”;
<code class="php">substr_replace(string $string, string $replacement, int $start, int $length)</code>
其中:
-
$string是要替换的字符串。 -
$replacement是替换字符串。 -
$start是替换开始的位置。 -
$length是要替换的长度。
strstr()
strstr() 函数在字符串中查找第一个出现的子字符串。语法如下:
<code class="php">strstr(string $string, string $substring)</code>
其中:
-
$string是要搜索的字符串。 -
$substring是要查找的子字符串。
strrpos()
strrpos() 函数在字符串中查找最后一个出现的子字符串。语法如下:
<code class="php">strrpos(string $string, string $substring)</code>
其中:
-
$string是要搜索的字符串。 -
$substring是要查找的子字符串。











