douyousu9691 2013-08-28 05:34
浏览 12
已采纳

搜索一个子字符串,如果它在最后,则返回true

I would like to search for a substring in php so that it will be at the end of the given string. Eg on string 'abd def' if I search for def it would be at the end, so return true. But if I search for abd it will return false since it is not at the end.

Is it possible?

  • 写回答

5条回答 默认 最新

  • dsfdsf21321 2013-08-28 05:39
    关注

    Assuming whole words:

    $match = 'def';
    $words = explode(' ', 'abd def');
    
    if (array_pop($words) == $match) {
      ...
    }
    

    Or using a regex:

    if (preg_match('/def$/', 'abd def')) {
      ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?