如何判断某个字符串中是否存在某个字符?

zjiong 2006-08-30 01:46:16
如何判断某个字符串中是否存在某个字符?
...全文
963 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
iasky 2006-08-30
  • 打赏
  • 举报
回复
ereg()
omoon 2006-08-30
  • 打赏
  • 举报
回复
PHP里用于查找或者匹配或者定位的函数里用得比较多的是strstr,stristr.
后者与前者的功能,返回值都一样,只是不区分大小写。

strstr("母字符串", "子字符串")用来查找子字符串在母字符串中第一次出现的位置,
并返回母字符串中从子字符串开始到母字符串结束的部分。
如果找不到子字符串,则返回空。
caotian2000 2006-08-30
  • 打赏
  • 举报
回复
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}

// We can search for the character, ignoring anything before the offset
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0
?>

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧