我正在使用脚本检查字符串 $ status (从另一个与API交互的脚本中随机生成)是否包含以下文本 -
SC-102
EOD-103
EOD-31
DLYDC-104
EOD-72
ST-108
SC-104
X-SC
EOD-114
DLYMR-118
EOD-76
EOD-6
EOD-95
EOD-69
EOD-16
EOD-2 的 EOD-97
EOD-110
EOD-15
EOD-32
EOD-107
EOD-106
EOD-46
EOD-104
EOD-42
EOD-7
EOD-111
EOD-43
我可以在代码中使用 OR 或 || 吗?
字母,特殊字符&数字是TEXT的一个单一部分。 对于。示例 - ST-102 是要从 $ status
进行搜索的确切文本我现在的代码如下 -
$codes = array("SC-102","EOD-103","EOD-31","DLYDC-104","EOD-72","ST-108","SC-104","X-SC","EOD-114","DLYMR-118","EOD-76","EOD-6","EOD-95","EOD-69","EOD-16","EOD-2","EOD-97","EOD-110","EOD-15","EOD-32","EOD-107","EOD-106","EOD-46","EOD-104","EOD-42","EOD-7","EOD-111","EOD-43");
if($status_code==$codes)
{
//My Script , if condition is true!
}
答案 0 :(得分:1)
使用explode
和in_array
功能实现
<?php
$string = 'SC-102 || EOD-103 || EOD-31 || DLYDC-104 || EOD-72 || ST-108 || SC-104 || X-SC || EOD-114 || DLYMR-118 || EOD-76 || EOD-6 || EOD-95 || EOD-69 || EOD-16 || EOD-2 || EOD-97 || EOD-110 || EOD-15 || EOD-32 || EOD-107 || EOD-106 || EOD-46 || EOD-104 || EOD-42 || EOD-7 || EOD-111 || EOD-43';
$codes = explode(' || ',$string);
$status_code = 'ST-108';
if(in_array($status_code, $codes)){
echo "Code Found\n";
}else{
echo "Code Not Found\n";
}
$status_code = 'ST-102';
if(in_array($status_code, $codes)){
echo "Code Found\n";
}else{
echo "Code Not Found\n";
}
?>
答案 1 :(得分:0)
你可以这样做
$codes = array("SC-102","EOD-103","EOD-31","DLYDC-104","EOD-72","ST-108","SC-104","X-SC","EOD-114","DLYMR-118","EOD-76","EOD-6","EOD-95","EOD-69","EOD-16","EOD-2","EOD-97","EOD-110","EOD-15","EOD-32","EOD-107","EOD-106","EOD-46","EOD-104","EOD-42","EOD-7","EOD-111","EOD-43");
$status_code = 'ST-108';
if(in_array($status_code,$codes))
{
//My Script , if condition is true!
}
我会帮助你。你可以检查Here
修改: - 强> 只需将if语句更改为
即可if(in_array($status_code,$codes))
答案 2 :(得分:0)
你可以搜索一个值是否存在于数组中。当你有字符串||时然后你需要explode,然后使用in_array。请尝试以下:
$values="SC-102 || EOD-103 || EOD-31 || DLYDC-104 || EOD-72 || ST-108 || SC-104 || X-SC || EOD-114 || DLYMR-118 || EOD-76 || EOD-6 || EOD-95 || EOD-69 || EOD-16 || EOD-2 || EOD-97 || EOD-110 || EOD-15 || EOD-32 || EOD-107 || EOD-106 || EOD-46 || EOD-104 || EOD-42 || EOD-7 || EOD-111 || EOD-43";
$codearr=explode("||",$values);
foreach($codearr as $status_code){
if(in_array($status_code,$codes)){
//found
}
else{
//not found
}
}
答案 3 :(得分:0)
请查看给定的脚本 -
$codes = array("SC-102","EOD-103","EOD-31","DLYDC-104","EOD-72","ST-108","SC-104","X-SC","EOD-114","DLYMR-118","EOD-76","EOD-6","EOD-95","EOD-69","EOD-16","EOD-2","EOD-97","EOD-110","EOD-15","EOD-32","EOD-107","EOD-106","EOD-46","EOD-104","EOD-42","EOD-7","EOD-111","EOD-43");
$status_code = 'ST-108';
if(in_array($status_code, $codes)){
echo 'Found';
}else
echo 'Not Found';