php - 在php中查找子字符串的最有效方法

标签 php

在招聘流程技能测试中,我被问到以下问题:

Assuming a large random string, eg:

$x = str_shuffle(str_repeat(implode('', range('a', 'z')), 1000000));

What's the most efficient way to find substrings on it?

我想知道答案只是为了了解它。

先谢谢了!

最佳答案

你可以这样做:

<?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";
}

关于php - 在php中查找子字符串的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35462833/

相关文章:

php - 在 Linux 上,我想停止对特定 Windows 服务器的身份验证,该服务器将使用它来使用 Sendmail 发送邮件

php - PHP 中 mysql 查询字符串的奇怪行为

php - 我想就 Paypal 表格征求专家的意见

Javascript 变量不可见

php - 当只有 1 个匹配项时搜索多个关键字的条形码

php - 在 MYSQL 中连接多个 (4) 表

php - 使用 PHP 通过 ODBC 运行 MSSQL 存储过程(并返回结果)

php - 我想从数据库中选择数据我的代码有什么错误,

php从数据库加载图像(pdf)

php - 根据ID将数据库表数据拆分到不同的数组中