如何在PHP中检查字符串是否以“_”开头?

时间:2009-12-25 21:50:28

标签: php

示例:我有一个$variable = "_foo",我想确保$ variable不以下划线"_"开头。我怎么能用PHP做到这一点?是否可以访问字符串后面的char数组?

7 个答案:

答案 0 :(得分:118)

$variable[0] != "_"

它是如何运作的?

在PHP中,您可以使用数组索引表示法获取字符串的特定字符。 $variable[0]是字符串的第一个字符(如果$ variable是一个字符串)。

答案 1 :(得分:103)

您可以在php中查看substr函数并抓取第一个字符:

http://php.net/manual/en/function.substr.php

if (substr('_abcdef', 0, 1) === '_') { ... }

答案 2 :(得分:46)

由于有人提到了效率,我已经基于好奇心对迄今给出的功能进行了基准测试:

function startsWith1($str, $char) {
    return strpos($str, $char) === 0;
}
function startsWith2($str, $char) {
    return stripos($str, $char) === 0;
}
function startsWith3($str, $char) {
    return substr($str, 0, 1) === $char;
}
function startsWith4($str, $char){
    return $str[0] === $char;
}
function startsWith5($str, $char){
    return (bool) preg_match('/^' . $char . '/', $str);
}
function startsWith6($str, $char) {
    if (is_null($encoding)) $encoding = mb_internal_encoding();
    return mb_substr($str, 0, mb_strlen($char, $encoding), $encoding) === $char;
}

以下是我的普通DualCore机器的结果,每台机器运行100.000次

// Testing '_string'
startsWith1 took 0.385906934738
startsWith2 took 0.457293987274
startsWith3 took 0.412894964218
startsWith4 took 0.366240024567 <-- fastest
startsWith5 took 0.642996072769
startsWith6 took 1.39859509468

// Tested "string"
startsWith1 took 0.384965896606
startsWith2 took 0.445554971695
startsWith3 took 0.42377281189
startsWith4 took 0.373164176941 <-- fastest
startsWith5 took 0.630424022675
startsWith6 took 1.40699005127

// Tested 1000 char random string [a-z0-9]
startsWith1 took 0.430691003799
startsWith2 took 4.447286129
startsWith3 took 0.413349866867
startsWith4 took 0.368592977524 <-- fastest
startsWith5 took 0.627470016479
startsWith6 took 1.40957403183

// Tested 1000 char random string [a-z0-9] with '_' prefix
startsWith1 took 0.384054899216
startsWith2 took 4.41522812843
startsWith3 took 0.408898115158
startsWith4 took 0.363884925842 <-- fastest
startsWith5 took 0.638479948044
startsWith6 took 1.41304707527

正如您所看到的,将haystack视为数组以找出第一个位置的char是始终是最快的解决方案。无论字符串长度如何,它也始终以相同的速度执行。对于短字符串,使用strpossubstr更快,对于长字符串,使用stripos更慢,当字符串不以前缀开头时。但差异无关紧要。使用长字符串preg_match 令人难以置信地慢。无论字符串长度如何,mb_substr执行的大致相同,但速度只是平庸。 startsWith解决方案表现最差,但可能更可靠。

鉴于这些数字是100.000次运行,显然我们说的是每次通话0.0000x秒。除非你的应用正在{{1}}检查生活,否则从效率中挑选一个是一个毫无价值的微优化。

答案 3 :(得分:8)

这是您不关心绩效的最简单的答案:

if (strpos($string, '_') === 0) {
    # code
}

如果strpos返回0,则表示您要查找的内容从字符0开始,即字符串的开头。

这里有详细记录:http://uk3.php.net/manual/en/function.strpos.php

(PS $string[0] === '_'是最佳答案)

答案 4 :(得分:4)

function starts_with($s, $prefix){
    // returns a bool
    return strpos($s, $prefix) === 0;
}

starts_with($variable, "_");

答案 5 :(得分:2)

这是一个更好的函数开头:

function mb_startsWith($str, $prefix, $encoding=null) {
    if (is_null($encoding)) $encoding = mb_internal_encoding();
    return mb_substr($str, 0, mb_strlen($prefix, $encoding), $encoding) === $prefix;
}

答案 6 :(得分:1)

以pinusnegra的答案为基础,并回应Gumbo对该答案的评论:

function has_leading_underscore($string) {

    return $string[0] === '_';

}

在PHP 5.3.0上运行,以下工作并返回预期值,即使不检查字符串的长度是否至少为1个字符:

echo has_leading_underscore('_somestring').', ';
echo has_leading_underscore('somestring').', ';
echo has_leading_underscore('').', ';
echo has_leading_underscore(null).', ';
echo has_leading_underscore(false).', ';
echo has_leading_underscore(0).', ';
echo has_leading_underscore(array('_foo', 'bar'));

/*
 * output: true, false, false, false, false, false, false
 */

我不知道其他版本的PHP会如何反应,但如果它们都有效,那么这种方法可能比substr路由更有效。