gpt4 book ai didi

php - switch 语句中的正则表达式

转载 作者:IT王子 更新时间:2023-10-28 23:59:53 24 4
gpt4 key购买 nike

PHP switch/case 语句中是否允许使用正则表达式以及如何使用它们?

最佳答案

Switch-case 语句的工作方式类似于 if-elseif。
除了可以将正则表达式用于 if-elseif 之外,您还可以在 switch-case 中使用它。

if (preg_match('/John.*/', $name)) {
// do stuff for people whose name is John, Johnny, ...
}

可以编码为

switch $name {
case (preg_match('/John.*/', $name) ? true : false) :
// do stuff for people whose name is John, Johnny, ...
break;
}

希望这对您有所帮助。

关于php - switch 语句中的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4043741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com