dongxiong5546 2014-09-27 00:29
浏览 66
已采纳

如何检查字符串是否包含存储在数组中的字符,但如果字符串包含不在数组中的任何字符,则返回false

Basically I'm checking days of the week from an array to a string based on an initialization of the day for instance:

<?php
$check_days = array("M", "T");
$days1 = "MTW";
$days2 = "M";
[insert code to compare $check_days to $days1 and $days2 so that $days1 returns FALSE while $days2 returns TRUE]
?>
  • 写回答

3条回答 默认 最新

  • duanbi3151 2014-09-27 00:34
    关注

    Try using a regex character class and implode:

    if (preg_match('/^['.implode($check_days).']+$/', $days1)) {
        // do some stuff
    }
    

    EDIT: Let me help explain what's going on here:

    implode($check_days)
    

    This combines all the elements of an array into a single string. In your case, this is "MT".

    preg_match('/^[MT]+$/', $days1);
    

    This is a regular expression that checks that after the 'beginning' (^), $days1 contains either an "M" or a "T" ([MT]), repeated one or more times (+), then the string ends ($). It returns true if this is the case.

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥50 成都蓉城足球俱乐部小程序抢票