在PHP中搜索字符串中包含特殊字符的特定文本

时间:2017-06-29 06:58:00

标签: php arrays if-statement search

我正在使用脚本检查字符串 $ 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!
}

4 个答案:

答案 0 :(得分:1)

使用explodein_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";
    }
?>

现场演示:https://eval.in/824205

答案 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';