Linux 拨号vps windows公众号手机端

PHP查找字符串的方法有哪几种

lewis 9年前 (2016-08-18) 阅读数 9 #程序编程
文章标签 PHP

在PHP中,查找字符串的方法有以下几种:

  1. strpos()函数:用于在字符串中查找一个子字符串,并返回子字符串第一次出现的位置。如果未找到子字符串,则返回false。
$string = 'Hello, World!';
$substring = 'World';
$position = strpos($string, $substring);
echo $position;
  1. strrpos()函数:用于在字符串中查找一个子字符串,并返回子字符串最后一次出现的位置。如果未找到子字符串,则返回false。
$string = 'Hello, World!';
$substring = 'o';
$position = strrpos($string, $substring);
echo $position;
  1. strstr()函数:用于在字符串中查找一个子字符串,并返回子字符串及其后面的所有内容。
$string = 'Hello, World!';
$substring = 'World';
$result = strstr($string, $substring);
echo $result;
  1. stristr()函数:用于在字符串中查找一个子字符串(不区分大小写),并返回子字符串及其后面的所有内容。
$string = 'Hello, World!';
$substring = 'world';
$result = stristr($string, $substring);
echo $result;
  1. substr()函数:用于从字符串中取出一部分内容,可以指定起始位置和长度。
$string = 'Hello, World!';
$substring = substr($string, 7, 5);
echo $substring;
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门