php:写这个的快捷方式?

发布于 2024-08-28 12:03:31 字数 201 浏览 8 评论 0原文

写这个的最短方法是什么?

if(strpos($haystack, $needle)!==false){
    $len = strpos($haystack, $needle)+strlen($needle);
}else{
    $len = 0;
}

我记得我在某处看到了一些快捷方式,可以同时检查和设置变量。

what would be the shortest way to write this?

if(strpos($haystack, $needle)!==false){
    $len = strpos($haystack, $needle)+strlen($needle);
}else{
    $len = 0;
}

I remember that I saw some shortcut somewhere for this that checked and set a variable at the same time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萌吟 2024-09-04 12:03:31
$len = 0;
if(($pos = strpos($haystack, $needle)) !== false) {
    $len = $pos + strlen($needle);
}

我建议不要使用三元 ?: 运算符,即使它更短。

$len = 0;
if(($pos = strpos($haystack, $needle)) !== false) {
    $len = $pos + strlen($needle);
}

I'd recommend against the ternary ?: operator, even if it is shorter.

冷心人i 2024-09-04 12:03:31
$len = strpos($haystack, $needle);
$len = ($len !== false) ? $len + strlen($needle) : 0;
$len = strpos($haystack, $needle);
$len = ($len !== false) ? $len + strlen($needle) : 0;
铁轨上的流浪者 2024-09-04 12:03:31
$len=strpos($haystack, $needle);
if($len !== FALSE) {
    $len +=  strlen($needle);
}

而且,在我看来,三元运算符对可读性影响很大。

$len=strpos($haystack, $needle);
if($len !== FALSE) {
    $len +=  strlen($needle);
}

and, in my opinion, ternary operator is terrible impact on readability.

我不会写诗 2024-09-04 12:03:31

它被称为 十元运算符,可以像这样使用:

$len = strpos($haystack, $needle) ? strpos($haystack, $needle)+strlen($needle) : 0;

不过,您应该小心使用它,因为它会使某些表达式变得难以阅读。

It is called the tenary operator and can be used like this:

$len = strpos($haystack, $needle) ? strpos($haystack, $needle)+strlen($needle) : 0;

You should use it with care though, because it can make some expressions quite hard to read.

红ご颜醉 2024-09-04 12:03:31
$len = strpos($haystack, $needle) ? strpos($haystack, $needle)+strlen($needle) : 0;

另请参阅三元运算

$len = strpos($haystack, $needle) ? strpos($haystack, $needle)+strlen($needle) : 0;

See also Ternary Operation.

无尽的现实 2024-09-04 12:03:31

一种不同的方法:

$len = strlen(preg_replace('/(.*?'.preg_quote($needle,'/').')?.*/', '$1', $haystack));

可能更慢并且更占用内存,但它确实需要更少的打字。所以它是否实际上是一个快捷方式取决于定义。如果您对三元运算符和求值条件下的赋值过敏,它确实提供了一个有效的选项。

您也可以这样做,

$len = preg_match('/'.preg_quote($needle,'/').'()/', $haystack, $m, PREG_OFFSET_CAPTURE)? $m[1][1] : 0

尽管使用 preg_ 函数来搜索固定字符串再次有点浪费资源。

A different approach:

$len = strlen(preg_replace('/(.*?'.preg_quote($needle,'/').')?.*/', '$1', $haystack));

Probably slower and more memory-intensive, but it does require less typing. So whether or not it's actually a shortcut depends on the definition. It does present a valid option if you're allergic to ternary operators and assignment within evaluation conditions.

You could also do

$len = preg_match('/'.preg_quote($needle,'/').'()/', $haystack, $m, PREG_OFFSET_CAPTURE)? $m[1][1] : 0

although again it's a bit wasteful of resources to be using preg_ functions to search for fixed strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文