在招聘流程技能测试中,我被问到以下问题:
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/