dongteng2534 2014-01-30 05: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 05: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部