dpzff20644 2017-11-23 05:55
浏览 44
已采纳

PHP:检查URL中给定字符串后是否有任何内容?

I need to create a an if statement that fires if there is something after a given string in the URL.

For example, if my string is 'Honda' I need to check if something appears after that word in the url, eg something like:

$brand = "honda";

if (url contains $brand . '/*'){
  // Do something.
}

Example urls could be:

  • mysite.com/honda (which would fail the above)
  • mysite.com/cars/honda (which would fail the above)
  • mysite.com/honda/civic (which would pass the above)
  • mysite.com/honda/accord (which would pass the above)

I know I can use strpos() to detect if the string is within the URL, but how can I detect if anything comes after that?

  • 写回答

1条回答 默认 最新

  • doutu1889 2017-11-23 05:58
    关注

    Use a regular expression that looks for the $brand at the end of the string.

    if (! preg_match("/{$brand}$/", $url)) {
        // ...
    }
    

    If you need to check if the $brand actually appears in the URL before running your end of string check:

    if (strpos($brand, $url) !== false && ! preg_match("/{$brand}$/", $url)) {
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥70 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52