doo6568 2013-02-10 15:53
浏览 11
已采纳

使用PHP检查URL中的某些单词

I am trying to write a php code, that checks if there is a certain word in the url and if so - show something.

I am using this one:

$url = parse_url($_SERVER['REQUEST_URI']);

if($url['path'] == '/main-category/') {
echo........

For example I am looking for "main-category". For http://www.site.com/main-category/ the code is working, but if the url contains subcategory http://www.site.com/main-category/sub-category/, it isn't.

How can I make it find /main-category/ no matter if there is something after it, or not?

I read some topics here but didn't figure it out.

  • 写回答

1条回答 默认 最新

  • douli7841 2013-02-10 15:54
    关注

    Use strpos(). Example from the manual:

    <?php
    $mystring = 'abc';
    $findme   = 'a';
    $pos = strpos($mystring, $findme);
    
    // Note our use of ===.  Simply == would not work as expected
    // because the position of 'a' was the 0th (first) character.
    if ($pos === false) {
        echo "The string '$findme' was not found in the string '$mystring'";
    } else {
        echo "The string '$findme' was found in the string '$mystring'";
        echo " and exists at position $pos";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序