dpmwy80068 2017-10-25 16:04
浏览 26

如何在字符串的末尾搜索文本中存在的文本?

For example I have this :

$string = 'PHP is a server side web programming language , PHP is amazing , I love PHP';

$array = array('html','css','javascript','ajax','html5','css3','jquery','PHP');

foreach($array as $ar){
   //Check if one of the $array values exists in the $string
}

I want to search from the end of the $string only , so if the $array value is not at the end then nothing would happen as it's not exist , The PHP could be any of the other values in the $array so I don't know the length the value should be found at , I mean the word could be repeated and with different length.

i.e : $string = 'html .... , html is fantastic , I love html'; , now the length of the word is bigger , and it could be bigger than that.

How to find the last one only what ever the length of the word is ?

  • 写回答

6条回答 默认 最新

  • duanna1407 2017-10-25 16:14
    关注

    Try using regex's closing anchor $ like this:

    $string = 'PHP is a server side web programming language , PHP is amazing , I love PHP';
    
    $array = ['html', 'css', 'javascript', 'ajax', 'html5', 'css3', 'jquery', 'PHP'];
    
    foreach ($array as $ar) {
        // Check if one of the $array values exists in the $string
        if (preg_match("/$ar$/", $string)) {
            echo "Found $ar at the end of \$string";
            break;
        }
    }
    

    This outputs:

    Found PHP at the end of $string
    

    eval.in demo

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测