dongteng2534 2014-01-30 13:14
浏览 23
已采纳

PHP使用STRPOS在CLASS中查找部分值

I need to search inside a string to find if there ise any matches to this pattern:

class="%men%"

It means that the class may be equal to either:

class="men"
class="mainmen"
class="MyMen" 
class="menSuper"
class="MyMenSuper"

etc

I need someting like strpos($string,'class="%men%') where % could be anything.

Best, Martti

  • 写回答

5条回答 默认 最新

  • douye4254 2014-01-30 13:18
    关注

    Try using preg_match

    if (preg_match("/men/i", $class)) {
        echo "A match was found.";
    } else {
        echo "A match was not found.";
    }
    

    Look at this link for more information http://www.php.net/preg_match

    Edit :

    So you can do like this (Inspired from the answer of Marius.C)

    $string = '<div class="menlook">Some text</div>';
    if (preg_match('/class="(.*)men(.*)"/i', $string)) {
        echo "A match was found.";
    } else {
        echo "A match was not found.";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝